Managing Docker Containers
Docker is OS-level virtualization software for developers and sysadmins, the beauty of docker is that it creates an individual tenant for every application that is deployed. A DockerFile is file is a script that is composed of a set of commands that will execute automatically for building new docker images. In this guide, we will make our own docker file. For the demo, we will keep it simple. We will be creating a centos based image.
# vi dockerfile
# Docker base on the latest CentOS 7
FROM centos:latest
MAINTAINER [email protected]
RUN useradd -ms /bin/bash user
USER user
Now we initiate the above script.
# docker build -t centos7/nonroot:v1 .
For launching an above docker script/file
# docker run -it centos7/nonroot:v1 /bin/bash
You will connect to docker container but you cannot enter as a root user for this we will issue a command to connect to docker container
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b45a206377e centos7/nonroot:v1 "/bin/bash" 2 hours ago Exited (1) 2 hours ago pedantic_banach
696797d4e72b centos7/nonroot:v1 "/bin/bash" 2 hours ago Exited (0) 2 hours ago stupefied_leakey
c54689519958 ubuntu:xenial "/bin/bash" 16 hours ago Up 16 hours thirsty_gates
0829a7b03aee hello-world "/hello" 11 days ago Exited (0) 11 days ago adoring_mirzakhani
After noting the name of container we will start it.
# docker start pedantic_banach
Now we know the container name, we will issue below command for user to attach as a root
# docker exec -u 0 -it pedantic_banach /bin/bash
How to expose a port on docker
For this we will pull nginx image from docker hub.
# docker pull nginx
Now for checking images
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7/nonroot v1 1b7fc6bf7fed 13 hours ago 196.9 MB
docker.io/ubuntu xenial 2d696327ab2e 2 days ago 121.6 MB
docker.io/centos latest 196e0ce0c9fb 6 days ago 196.6 MB
docker.io/nginx latest da5939581ac8 7 days ago 108.3 MB
docker.io/hello-world latest 1815c82652c0 3 months ago 1.84 kB
For running a container from image use the below command.
# docker run -d nginx:latest
For confirming that nginx container has been started use the below command.
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23d81c345e00 nginx:latest "nginx -g 'daemon off" 6 seconds ago Up 5 seconds 80/tcp sharp_torvalds
0b45a206377e centos7/nonroot:v1 "/bin/bash" 13 hours ago Up 10 hours pedantic_banach
696797d4e72b centos7/nonroot:v1 "/bin/bash" 13 hours ago Exited (0) 13 hours ago stupefied_leakey
c54689519958 ubuntu:xenial "/bin/bash" 26 hours ago Uphours thirsty_gates
0829a7b03aee hello-world "/hello" 11 days ago Exited (0) 11 days ago adoring_mirzakhan
To find out the IP Address of the container
# docker inspect sharp_torvalds | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.4",
"IPAddress": "172.17.0.4",
Note the IP address of docker container. We will test the nginx application on elink browser which is a utility of linux core version.
# yum install elinks -y
# elinks http://172.17.0.4
This confirms that Nginx is running fine, but we are accessing it from the container on base machine (centos 7). In the next step, we will expose the port of Nginx to base machine, so when we put in “elinks http://localhost” on base machine, it will show us the Nginx default page. First, we will stop the nginx container, and then we will expose the port.
# docker stop sharp_torvalds
# docker run -d -p 8080:80 nginx:latest
Note: “-p” denotes the port number
Now we have exposed the port of nginx to the base machine, but when we run the container again it will change the name of the container, so for confirming container name we use
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20dddf7697ce nginx:latest "nginx -g 'daemon off" 4 seconds ago Up 3 seconds 0.0.0.0:8080->80/tcp goofy_shirley
23d81c345e00 nginx:latest "nginx -g 'daemon off" 23 minutes ago Exited (0) 6 minutes ago sharp_torvalds
0b45a206377e centos7/nonroot:v1 "/bin/bash" 13 hours ago Up 11 hours pedantic_banach
696797d4e72b centos7/nonroot:v1 "/bin/bash" 13 hours ago Exited (0) 13 hours ago stupefied_leakey
c54689519958 ubuntu:xenial "/bin/bash" 27 hours ago Up 27 hours thirsty_gates
0829a7b03aee hello-world "/hello" 11 days ago Exited (0) 11 days ago adoring_mirzakhani
Now for confirming that nginx is accessible.
# elinks http://localhost:8080
This is for basic container management.