Docker Container Commands - create, ls, run, stop, restart, pause, unpause, logs, kill

All the important docker container commands for your quick reference. Bookmark this page for quick docker container commands.

Create Container:

docker create [docker_image]

Example: To create a container from an image named myapp:

docker create myapp

Run the docker image in a new container:

docker run [docker_image]
For example:
docker run -p 8080:8080 springboot-docker-image
-p option to map host port with container port.

Give a name to the container:

docker run --name <container-name> <image-name>
For example:
docker run --name springboot-docker-container -p 8080:8080 springboot-docker-image

Run container in background and print container ID:

docker run -d <docker-image>
-d option to run the container in detached mode (background).

For example:

docker run -d -p 8080:8080 springboot-docker-image

Fetch the logs of a container

docker logs -f <container-id>/<container-name>
For example:
docker logs -f springboot-docker-container

List all the docker running containers:

docker ps
OR
docker container ls

Docker container pause:

docker container pause <container-id>/<container-name>
For example:

docker container pause springboot-docker-container

Docker container unpause:

docker container unpause <container-id>/<container-name>
For example:

docker container unpause springboot-docker-container

Kill docker container:

docker kill <container-id>/<container-name>
For example:

docker kill springboot-docker-container
This command kills the container by stopping its execution immediately. The difference between ‘docker kill’ and ‘docker stop’ is that ‘docker stop’ gives the container time to shutdown gracefully, in situations when it is taking too much time for getting the container to stop, one can opt to kill it.

Stop running container:

docker stop <container-id>/<container-name>
For example:

docker stop springboot-docker-container

List all running and exited containers:

docker container ls -a

Remove all stopped containers:

docker container prune

Restarting a Single Container:

To restart a single container, simply specify its name or ID:

docker restart my_container_name

Restarting Multiple Containers:

You can list down multiple container names or IDs:

docker restart my_container_1 my_container_2 my_container_3

Related Tutorials

Comments