Day 29: Docker - Running and managing containers
1 min read
Containers are lightweight, isolated environments that encapsulate applications, making them portable and easy to deploy. Docker provides powerful commands to run, monitor, and manage them — here is how I learned to use them.
Running a Docker container
To run a container you need an image as a blueprint. docker run starts a container from it, pulling the image first if it is not available locally:
$ docker run -d -p 8080:80 --name my-nginx nginx:latest
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
f03b40093957: Pull complete
eed12bbd6494: Pull complete
fa7eb8c8eee8: Pull complete
7ff3b2b12318: Pull complete
0f67c7de5f2c: Pull complete
831f51541d38: Pull complete
Digest: sha256:af296b188c7b7df99ba960ca614439c99cb7cf252ed7bbc23e90cfda59092305
Status: Downloaded newer image for nginx:latest
d90a8f6684b2e0820878bbd575d553e82f3976c61ff593ad7fa93f1538c7ef40
- The
-dflag runs the container in detached mode, in the background. - The
-pflag publishes container port 80 on host port 8080, so the Nginx server is reachable athttp://localhost:8080. - The
--nameflag names the containermy-nginx. nginx:latestpulls the latest Nginx image from Docker Hub.
Managing Docker containers
Once containers are running, these commands manage and monitor them:
docker ps— lists running containers with ID, image, status, ports, and names.docker stop <container>— gracefully stops a running container (ID or name).docker start <container>— starts a stopped container.docker restart <container>— stops and starts a running container again.docker logs <container>— shows the output and logs of a container’s processes.docker exec -it <container> <command>— runs a command inside a running container;-itopens an interactive terminal.docker rm <container>— permanently deletes a stopped container.
Example
# Run the Nginx container in detached mode, mapping host port 7400 to container port 80
$ docker run -d -p 7400:80 --name my-nginx nginx:latest
786cddb4675284e38479da5592f03179ffeea651e1423f38db832cf86a3eccac
# List the running containers
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
786cddb46752 nginx:latest "/docker-entrypoint.…" 7 seconds ago Up 3 seconds 0.0.0.0:7400->80/tcp my-nginx
ac4d4ba5db97 postgres:latest "docker-entrypoint.s…" 6 days ago Up 6 hours 0.0.0.0:5432->5432/tcp postgres
f76dc8e0a2ae docker:dind "dockerd-entrypoint.…" 6 days ago Up 6 hours 2375/tcp, 0.0.0.0:2376->2376/tcp jenkins-docker
e676b19a399b myjenkins-blueocean:2.387.3-1 "/usr/bin/tini -- /u…" 6 days ago Up 6 hours 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp jenkins-blueocean
# Stop the my-nginx container
$ docker stop my-nginx
my-nginx
# List the running containers (my-nginx is stopped, so it doesn't show up)
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ac4d4ba5db97 postgres:latest "docker-entrypoint.s…" 6 days ago Up 6 hours 0.0.0.0:5432->5432/tcp postgres
f76dc8e0a2ae docker:dind "dockerd-entrypoint.…" 6 days ago Up 6 hours 2375/tcp, 0.0.0.0:2376->2376/tcp jenkins-docker
e676b19a399b myjenkins-blueocean:2.387.3-1 "/usr/bin/tini -- /u…" 6 days ago Up 6 hours 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp jenkins-blueocean
# List all containers (including stopped ones)
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
786cddb46752 nginx:latest "/docker-entrypoint.…" 26 seconds ago Exited (0) 9 seconds ago my-nginx
ac4d4ba5db97 postgres:latest "docker-entrypoint.s…" 6 days ago Up 6 hours 0.0.0.0:5432->5432/tcp postgres
f76dc8e0a2ae docker:dind "dockerd-entrypoint.…" 6 days ago Up 6 hours 2375/tcp, 0.0.0.0:2376->2376/tcp jenkins-docker
e676b19a399b myjenkins-blueocean:2.387.3-1 "/usr/bin/tini -- /u…" 6 days ago Up 6 hours 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp jenkins-blueocean
# Start the my-nginx container
$ docker start my-nginx
my-nginx
# List the running containers
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
786cddb46752 nginx:latest "/docker-entrypoint.…" 38 seconds ago Up 2 seconds 0.0.0.0:7400->80/tcp my-nginx
ac4d4ba5db97 postgres:latest "docker-entrypoint.s…" 6 days ago Up 6 hours 0.0.0.0:5432->5432/tcp postgres
f76dc8e0a2ae docker:dind "dockerd-entrypoint.…" 6 days ago Up 6 hours 2375/tcp, 0.0.0.0:2376->2376/tcp jenkins-docker
e676b19a399b myjenkins-blueocean:2.387.3-1 "/usr/bin/tini -- /u…" 6 days ago Up 6 hours 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp jenkins-blueocean
# Restart the my-nginx container
$ docker restart my-nginx
my-nginx
# View the logs of the my-nginx container
$ docker logs my-nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/05/28 12:53:11 [notice] 1#1: using the "epoll" event method
2023/05/28 12:53:11 [notice] 1#1: nginx/1.25.0
2023/05/28 12:53:11 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/05/28 12:53:11 [notice] 1#1: OS: Linux 5.15.90.1-microsoft-standard-WSL2
2023/05/28 12:53:11 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/05/28 12:53:11 [notice] 1#1: start worker processes
2023/05/28 12:53:11 [notice] 1#1: start worker process 28
2023/05/28 12:53:11 [notice] 1#1: start worker process 29
2023/05/28 12:53:11 [notice] 1#1: start worker process 30
2023/05/28 12:53:22 [notice] 1#1: signal 3 (SIGQUIT) received, shutting down
2023/05/28 12:53:22 [notice] 28#28: gracefully shutting down
2023/05/28 12:53:22 [notice] 28#28: exiting
2023/05/28 12:53:22 [notice] 28#28: exit
2023/05/28 12:53:22 [notice] 29#29: gracefully shutting down
2023/05/28 12:53:22 [notice] 29#29: exiting
2023/05/28 12:53:22 [notice] 29#29: exit
2023/05/28 12:53:22 [notice] 30#30: gracefully shutting down
2023/05/28 12:53:22 [notice] 30#30: exiting
2023/05/28 12:53:22 [notice] 30#30: exit
2023/05/28 12:53:23 [notice] 1#1: signal 17 (SIGCHLD) received from 28
2023/05/28 12:53:23 [notice] 1#1: worker process 28 exited with code 0
2023/05/28 12:53:23 [notice] 1#1: signal 29 (SIGIO) received
2023/05/28 12:53:23 [notice] 1#1: signal 17 (SIGCHLD) received from 30
2023/05/28 12:53:23 [notice] 1#1: worker process 30 exited with code 0
2023/05/28 12:53:23 [notice] 1#1: signal 29 (SIGIO) received
2023/05/28 12:53:23 [notice] 1#1: signal 17 (SIGCHLD) received from 29
2023/05/28 12:53:23 [notice] 1#1: worker process 29 exited with code 0
2023/05/28 12:53:23 [notice] 1#1: exit
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/05/28 12:53:42 [notice] 1#1: using the "epoll" event method
2023/05/28 12:53:42 [notice] 1#1: nginx/1.25.0
2023/05/28 12:53:42 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/05/28 12:53:42 [notice] 1#1: OS: Linux 5.15.90.1-microsoft-standard-WSL2
2023/05/28 12:53:42 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/05/28 12:53:42 [notice] 1#1: start worker processes
2023/05/28 12:53:42 [notice] 1#1: start worker process 22
2023/05/28 12:53:42 [notice] 1#1: start worker process 23
2023/05/28 12:53:42 [notice] 1#1: start worker process 24
2023/05/28 12:53:53 [notice] 1#1: signal 3 (SIGQUIT) received, shutting down
2023/05/28 12:53:53 [notice] 23#23: gracefully shutting down
2023/05/28 12:53:53 [notice] 22#22: gracefully shutting down
2023/05/28 12:53:53 [notice] 22#22: exiting
2023/05/28 12:53:53 [notice] 23#23: exiting
2023/05/28 12:53:53 [notice] 22#22: exit
2023/05/28 12:53:53 [notice] 23#23: exit
2023/05/28 12:53:53 [notice] 24#24: gracefully shutting down
2023/05/28 12:53:53 [notice] 24#24: exiting
2023/05/28 12:53:53 [notice] 24#24: exit
2023/05/28 12:53:54 [notice] 1#1: signal 17 (SIGCHLD) received from 22
2023/05/28 12:53:54 [notice] 1#1: worker process 22 exited with code 0
2023/05/28 12:53:54 [notice] 1#1: signal 29 (SIGIO) received
2023/05/28 12:53:54 [notice] 1#1: signal 17 (SIGCHLD) received from 23
2023/05/28 12:53:54 [notice] 1#1: worker process 23 exited with code 0
2023/05/28 12:53:54 [notice] 1#1: signal 29 (SIGIO) received
2023/05/28 12:53:54 [notice] 1#1: signal 17 (SIGCHLD) received from 24
2023/05/28 12:53:54 [notice] 1#1: worker process 24 exited with code 0
2023/05/28 12:53:54 [notice] 1#1: exit
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/05/28 12:54:01 [notice] 1#1: using the "epoll" event method
2023/05/28 12:54:01 [notice] 1#1: nginx/1.25.0
2023/05/28 12:54:01 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/05/28 12:54:01 [notice] 1#1: OS: Linux 5.15.90.1-microsoft-standard-WSL2
2023/05/28 12:54:01 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/05/28 12:54:01 [notice] 1#1: start worker processes
2023/05/28 12:54:01 [notice] 1#1: start worker process 22
2023/05/28 12:54:01 [notice] 1#1: start worker process 23
2023/05/28 12:54:01 [notice] 1#1: start worker process 24
# Access the shell of the my-nginx container
$ docker exec -it my-nginx /bin/bash
root@786cddb46752:/# curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
root@786cddb46752:/# exit
exit
# Attempting to remove a running container (my-nginx) results in an error because the container is still running. It needs to be stopped before removal.
$ docker rm my-nginx
Error response from daemon: You cannot remove a running container 786cddb4675284e38479da5592f03179ffeea651e1423f38db832cf86a3eccac. Stop the container before attempting removal or force remove
# Stop my-nginx container
$ docker stop my-nginx
my-nginx
# Now remove works
$ docker rm my-nginx
my-nginx
# We don't see my-nginx container anymore
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ac4d4ba5db97 postgres:latest "docker-entrypoint.s…" 6 days ago Up 6 hours 0.0.0.0:5432->5432/tcp postgres
f76dc8e0a2ae docker:dind "dockerd-entrypoint.…" 6 days ago Up 6 hours 2375/tcp, 0.0.0.0:2376->2376/tcp jenkins-docker
e676b19a399b myjenkins-blueocean:2.387.3-1 "/usr/bin/tini -- /u…" 6 days ago Up 6 hours 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp jenkins-blueocean
Reference: