Introduction to Docker - Part 2

Introduction to Docker - Part 2

Written by Bobby Iliev on Apr 11th, 2020 Views Report Post

Introduction

Welcome to part 2 of my Introduction to Docker blog post series! In part 1 I quickly went through installing docker and what containers are. If you've not read the previous post I encourage you to do so:

https://devdojo.com/tutorials/introduction-to-docker-part-1

In this post, I'll show you how to start working with containers! We will pull a Docker image from the DockerHub, we will run a container, stop it, destroy it and more!

I'll be using DigitalOcean for all of the demos, so I would strongly encourage you to create a DigitalOcean account follow along. You would learn more by doing!

To make things even better you can use my referral link to get a free $100 credit that you could use to deploy your own servers and test the guide yourself on a few DigitalOcean Droplets:

Digital Ocean $50 Free Credit

Once you have your account here's how to deploy your first droplet/server:

https://www.digitalocean.com/docs/droplets/how-to/create/

I'll be using Ubuntu 18.04 so I would recommend that you stick to the same so you could follow along!

Running a container

Once you have your Ubuntu Droplet ready, ssh to the server and follow along!

So let's run our first Docker container! To do that you just need to run the following command:

docker run hello-world

You will get the following output:

We just ran a container based on the hello-world Docker Image, as we did not have the image locally, docker pulled the image from the DockerHub and then used that image to run the container. 

All that happened was: the container ran, printed some text on the screen and then exited.

Then to see some information about the running and the stopped containers run:

docker ps -a

You will see the following information for your hello-world container that you just ran:

root@docker:~# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
62d360207d08        hello-world         "/hello"            5 minutes ago       Exited (0) 5 minutes ago                       focused_cartwright

Then to list the locally available images on your host run:

docker images

Pulling an image from Docker Hub

Let's run a more useful container like Apache container for example. 

First, we can pull the image from the docker hub with the docker pull command:

docker pull webdevops/php-apache

You will see the following output:

Then we can get the image ID with the docker images command:

docker images

The output would look like this:

After that we can use the docker run command again:

docker run -d -p 80:80 IMAGE-ID

Quick rundown of the arguments that I've used:

    -d - it specifies that I want to run the container in the background. That way when you close your terminal the container would remain running.

    -p 80:80 - this means that the traffic from the host on port 80 would be forwarded to the container. That way you could access the apache instance which is running inside your docker container directly via your browser.

The output of the above command would look like this:

With the docker info command now we can see that we have 1 running container. And with the docker ps command we could see some useful information about the container like the container ID, when the container was started and etc:

root@docker:~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                                   NAMES
7dd1d512b50e        fd4f7e58ef4b        "/entrypoint supervi…"   About a minute ago   Up About a minute   443/tcp, 0.0.0.0:80->80/tcp, 9000/tcp   pedantic_murdock

Stopping and restarting a Docker Container

Then you can stop the running container with the docker stop command followed by the container ID:

docker stop CONTAINER_ID

If you need to you can start the container again:

docker start CONTAINER_ID

In order to restart the container you can use the following:

docker restart CONTAINER_ID

Accessing a running container

If you need to attach to the container and run some commands inside the container sue the docker exec command:

docker exec -it CONTAINER_ID /bin/bash 

That way you will get to a bash shell in the container and execute some commands inside the container itself. 

Then to detach from the interactive shell press CTRL+PQ that way you will not stop the container.


Deleting a container

To delete the container run first make sure that the container is not running and then run:

docker rm id

If you would like to delete the container and the image all together, just run:

docker rmi IMAGE_ID

Conclusion

That is pretty much it! Now you know how to pull Docker images from the Docker Hub, run, stop, start and even attach to Docker containers!

We are ready to move to Part 3 of the Introduction to Docker blog post series:

https://devdojo.com/tutorials/introduction-to-docker-part-3

Let me know if you have any questions!

Bobby

Comments (0)