Docker, the most popular container technology nowadays. By nature it’s an application container, means if you want apache you will get apache inside your container. Or if you want to run mysql inside your container its mysql only. Got the picture? Cool. In this post I ll show you how you can start with docker container. But before make your hands dirty, let’s see how docker work.
:: How its works ::
docker containers are based on image. Image is a read only entity which you cannot change. You can take a base image and in that image you can add other functionality/package. When you run a container your docker daemon first look for the image on your local machine, if it doesn’t find any image then it goes to the online image repository for docker call docker hub. Once it finds your image, it will download that image and make a local copy available. So next time when you run another container with the same image the docker will take that image from your local machine. So that’s the basic of how docker works. There are other things you should know about docker which I’ll tell you time to time. I’ll cover the below topics in this post:
- Managing Docker container
- Docker volumes
- Docker networking
- Docker Hub
- Docker Swarm
Let’s make our hands dirty with docker.
Docker comes with two edition ce (Community Edition) and ee (Enterprise Edition). Community edition is the free edition which we will use in this tutorial. I ll use an ubuntu 18.04 LTS server for this lab.
Type the below command to install the docker-ce in your host.
curl -fsSL get.docker.com -o get-docker.sh
this will download a script called “get-docker.sh” in your current directory. Type the below command to run the script which will install the docker-ce in your host.
sudo sh get-docker.sh
after the installation run the below command. The command will add your current user to the docker group so that you can run docker commands without sudo privileges.
sudo usermod -aG docker your-user
After that, run the following command to check then docker version
1.1: Run the first docker container
You have successfully installed the docker-ce in your host now it’s time to run our first container. Type the below command to run the “hello-world” docker container.
docker run hello-world
you will see some activity in your screen. Keep patient and watch carefully. After sometime you will see a message like the below one:
Hello from Docker! This message shows that your installation appears to be working correctly.
And also some other message which simply describes what happen when you run the docker run command.
1.2: The busybox container
Let’s run another container name “busybox”. You can pass any command during the runtime and it will give you the output of that command. Type the below command to run the busybox container.
docker run busybox echo "Hello docker"
you can pass any other command instead of echo command. Try to run the container again with other command you like.
2: Managing docker container
Now we will see the docker ps command. A very useful command to see the status of a container. We will see how to download docker image and how to run container using that downloaded image. We will also see how to enter and exit from a container. How to start stop killing and finally how to delete a container and image.
2.1: docker ps command
Run the below command to check the status of the container. So far we have run some container, Let’s check the status of our container.
This command will print an empty list on your screen. Means no container is running at this stage. To see the previously run container, type the below command:
docker ps -a
This will give you a list of containers which you run before.
2.2: Docker images
As you already know that the container are images based and we already run some container. You can find the image information of those containers by typing the below command:
This will give you the list of downloaded images while running the container.
2.3: Download docker image
Docker download the respective image during the runtime of a container, but you can download your desire image if you want. You can also perform a search for the specific image. Type the below command to see a list of images docker have in its online repository docker hub. Let’s try to search the ubuntu image.
docker search ubuntu
You will see a list of Ubuntu images available online on docker hub. After getting your desire image it’s time to download that image. Type the below command to download the Ubuntu image.
docker pull ubuntu
The command will download the Ubuntu latest image available in docker hub. Check the newly downloaded image in your local repository. Again type the below command to see the local image list.
This will give you locally download image. if you want to learn more about your downloaded image then type the below command.
docker image inspect ubuntu
You will find the detail information about the image.
2.4: Run Ubuntu container with shell access
After download the Ubuntu image lets run a container and access that container using that image. Type the below command for create a running container.
docker run -it ubuntu bash
After running that command you will find yourself inside a running container. You can check the Ubuntu version by using the cat command.
Now let’s exit from the container shell. Type the exit command to exit from the container. Use the docker ps command to see the status of the container. Again, you will get an empty list. Do you know why?
2.5: Exit from the container
Every time you run the docker run command docker creates a container and put you in that container. But when you exit from that container the container stops. That’s the reason every time you get an empty list with the docker ps command. If you exit from the container the container will stop, but we want to keep our container running. Once again run another container.
docker run -it ubuntu bash
The container should start and you are now inside that container. Type the below key sequences for exit from the container without stopping it.
Now check the docker ps command again. There should be a running container now.
2.6: Docker start/stop/kill
You can stop a running container and similarly you can start a stop container. We now have some stop containers in our system. Find the list of the stopped container by docker ps – a command. From that list you can start your container. Type the below command to start a stop container.
docker start <contaierid/names>
If you want to stop or kill the container you can stop it by typing the below command.
docker stop/kill <contaierid/names>
The stop command sends a shutdown signal to the container. It will try to shut down the container in a proper way. The kill command sends a kill signal to the container which then immediately kill the container. kill is faster than stop.
2.7: Delete the container
After running some busybox container you are going to have a lot of stop container. To remove those containers run the below command:
docker rm container_id/container_name
To delete all the container all at a times type the below command:
docker rm $(docker ps -a -f status=exited -q)
2.8: Delete docker image
You can delete docker images from your docker host by typing the below command
docker rmi <imagename>
3: Docker volumes
Now we will see how docker handles volume. Volumes are used by docker container for persistent data. Volumes can be mounted in different way and can be shared among the container. We will see how to mount a volume, share the mounted volume among the containers.
3.1: Volumes and bind mounts
Volumes in docker are responsible for handling the data. You can create a container and can delete a container within a second but do you think what happen if you have data in that container? How you will make your data persistent? Or how you can share your data among other containers?
Docker has two options for containers to store files on the host machine, so that the files are persisted even after the container stops. They are volumes, and bind mounts. If you’re running Docker on Linux, you can also use a tmpfs mount. In this lab we will see how you can use volumes and bind mount inside a container.
Type the below command to create a volume name data-vol1
docker volume create data-vol1
type the below command to check the volume.
docker volume ls
you should see your newly created volume data-v1ol1. You can also find your volume on /var/lib/docker/volumes directory. To know the size and other information about the volume type the below command.
docker system df -v
Let’s run a simple ubuntu container using this volume.
docker run -it --mount source=data-vol1,target=/data ubuntu
You should now be inside the container. Type the ls command. You should see a data directory name data inside your container. Now make a file in the data directory.
cd /data && touch my-file && echo This is a file inside volume > my-file
Exit from that container. The container will stop. If you want your data again run another container with the volume data-vol1. You will get your data again.
3.2: Sharing data between container
You can share one volume with multiple container. Just run the below command twice, you will get 2 container sharing the same volume data-vol1 inside data directory.
docker run -it --mount source=data-vol1,target=/data ubuntu
You can create, delete, edit files in that volume. After doing your editing, exit from the container.
3.3: Read-only mount
Containers sharing the same volume does not maintain the file locking feature. All containers using the volume can write data at a same time, as a result chance of file corruption is high. To overcome this issue, you can mount a volume as read-only. Type the below command to mount a volume read-only.
docker run -it --mount source=data-vol1,target=/data,readonly ubuntu
Now try to create a directory inside the /data directory and see what happen.
3.4: Remove volumes
To remove a volume, you should be very careful because inside your volume you have your data and once you remove the volume your data will also be removed. Before deleting the volume make sure that the volume is not used with any container (stop/running). Type the below command to remove a volume.
docker volume rm data-vol1
To delete all volumes, type the below command
docker volume prune
3.5: Bind mount
Bind mounts are relatively easy and have been around early days of Docker. When you use a bind mount, a file or directory on the host machine is mounted in a container. Follow the below step to perform a bind mount.
- Create a directory in your host machine data.
2. Run a container and mount the data directory to the container.
docker run -it --mount type=bind,source="$(pwd)"/data,target=/app ubuntu
4: Docker networking
Now we will see the bridge networking drives provided by docker. We will create our own network and we will see how a docker container communicates with the outside world with exposing ports.
4.1: Network types
Docker comes with different network drivers. The most common network types are bridge, overlay and macvlan.
By default, docker creates “docker0” interface on the host machine during installation time. This docker0 interface is a bridge device for docker. All traffic from the docker containers flows over it to the Docker daemon, which handles routing on behalf of the container. If you don’t specify any network type all containers will connect to this device.
Before doing anything, let’s see what types of networking driver are available in the system. Type the below command to list the currently available network on the docker host machine.
docker network ls NETWORK ID NAME DRIVER SCOPE 0a5048ffd124 bridge bridge local b548da2c88a1 host host local 7c6ce4cef89c none null local
You will see an output like above (the network ID will not be the same). The default bridge network is listed along with host and none.
Let’s see some more detail information on the default bridge network. Type the below command to get more information about the default bridge network.
docker network inspect bridge
You will see the subnet and the gateway information for this default network. Now let’s run another container and see how this output look like. This time we will run alpine image.
docker run -it --name node1 alpine
This will run a new container based on alpine image. Let’s see what is the IP address of this container by providing ifconfig command inside the container. Exit from the container by pressing Ctrl+pq and run another alpine container.
docker run -it --name node2 alpine
Check the IP address of the new container and also check that the both containers can communicate with each other. You can inspect the bridge network again for more information.
4.2: New bridge network
It is possible to create your own customize bridge network. Type the below command to create your own bridge network.
docker network create -d bridge sanognet
Check the network list again by typing the below command.
docker network ls NETWORK ID NAME DRIVER SCOPE 0a5048ffd124 bridge bridge local b548da2c88a1 host host local 7c6ce4cef89c none null local c640505a8203 sanognet bridge local
As you can see the newly create network sanognet is available in the system. Let’s inspect the network by typing the below command. It will give you the IP and other information about the network.
docker network inspect sanognet
Let’s run a container and connect that container on our newly created network.
docker run -it --rm --name=node1 --net=sanognet alpine
check the IP address of the container.
4.3: Customize the IP address
It is also possible to create a new network with the choice of your IP address pool. Type the below command to create another bridge network with the customize IP address.
docker network create --subnet=192.168.10.0/24 --ip-range 192.168.10.0/29 bdnognet
Run a container and connect that container in this newly created network.
4.4: Bind container port
By default, all docker containers can connect to the outside world, but the outside world cannot connect to the container. To connect the outside world to your docker container you can use the –expose <port> command line flags. Run the below command to create a container using the nginx image with exposing the 8080 port. First let’s download the nginx image.
docker pull nginx
After downloading the image let’s run a container using this image.
docker run --name=web1 -p 80 -d nginx
Type the docker ps command and see the PORTS. You will see a random port is assigned by docker daemon. You can access this container from outside your host using this port number. Open a browser or curl to access this container.
http://<IP ADDRESS OF YOUR DOCKER HOST>:<PORT>
You can also define your port if you want. Just type the below command to assign your chosen port to the container.
docker run --name=web2 -p 8080:80 -d nginx
Type the docker ps command to find out your port.
Docker has a variety of image collection in docker hub, which can be downloaded easily and can be used by anyone. But you can create your own custom image using the Dockerfile. Dockerfile is a set of instruction which simply tell docker how to build an image. In this lab we will see how we can create our own customized image using the Dockerfile.
5.1: The Dockerflle
Let’s create a custom image based on our previously downloaded image Ubuntu.
Before doing that let’s run a new container based on Ubuntu image. After running the container try the below command inside that container.