Docker Command
docker run redis
// Run a container from an image with the latest version
docker run redis:4.0
// Add a tag to run a specific version
docker run -it -d ubuntu bash
root@dc43c98b31c9:/#
// -d means will run in detach mode and let the container run in the background
// the opposite of detach is docker attach ${containerName}
// -it for interactivity (prompt and waiting for STDIN)
// And hash value that is generated randomly by docker to identify which
// containers is which.
docker ps
// To see all the containers that are currently running in background
dockers ps -a
// To check all running and exited containers
docker exec -it ${containerId or names} bash
// To run the running container in the background.
docker stop ${containerId or names}
// To stop container running in the background
docker rm ${containerId or names}
// To delete exited container
docker images
// To list of available images
docker rmi ${image name}
// To remove images. Must ensure all dependencies are removed first
docker pull ${image name}
// Will only pull the image and not run the container
docker inspect ${container name or container id}
// Give more detail on specific container in json format
// Can be used also to inspect Env Variable used to run the container
docker logs ${container name or container id}
// logs for container running in the background (detached mode)
docker run -e APP_COLOR=green simple-webapp-color
docker run -e APP_COLOR=blue simple-webapp-color
// -e for environment variable
docker run -p 38282:8080 --name blue-app -e APP_COLOR=blue -d kodekloud/simple-webapp
// 8080 to 38282
// name container: blue-app
// env APP_COLOR=blue
// detached mode of images kodekloud/simple-webapp
// To know the env field from within a blue-app container,
// run docker exec -it blue-app envAfter creating API server using Docker container, we can give this folder with Dockerfile to anyone and as long as they have Docker, they can simply run this folder on any machine that we want.
When you run an image, it exit immediately. Why? Because container is not supposed to host an operating system (like VM). It just run specific task or process such as instance of web server or application server or database, etc. Once the task is complete, the container exits. Only lives as long as the process inside it is alive.
Last updated
Was this helpful?