Introduction to Docker - Part 3
Welcome to part 3 of my Introduction to Docker blog post series! In part 1 I quickly went through installing Docker and what containers are and in Part 2 we actually played with Docker containers a little bit by running a couple of Docker containers. If you've not read the previous posts I encourage you to do so:
In this post I'll show you how to work with Docker images!
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 $50 credit that you could use to deploy your virtual machines and test the guide yourself on a few Digital Ocean servers:
Once you have your account here's how to deploy your first droplet/server:
I'll be using Ubuntu 18.04 so I would recommend that you stick to the same so you could follow along.
Working with images
The docker run command downloads and runs images at the same time. But we could also only download images if we wanted to wit the docker pull command. For example:
docker pull ubuntu
Or if you want to get a specific version you could also do that with:
docker pull ubuntu:14.04
Then to list all of your images use the docker images command:
You would get a similar output to:
The images are stored locally on your docker host machine.
To take a look a the docker hub go to: https://hub.docker.com and you would be able to see where the images were just downloaded from.
For example here's a link to the Ubuntu image that we've just downloaded:
There you could find some useful information.
To delete an image use the docker rmi command:
docker rmi ubuntu:14.04
Modifying images ad-hoc
One of the ways of modifying images is with ad-hoc commands. For example just start your ubuntu container
docker run -d -p 80:80 IMAGE-ID
Attach to your container:
docker exec -it <container name> /bin/bash
Install whatever needed then exit the container and run:
docker container commit ID_HERE
List your images and note your image ID:
docker images ls
The process would look something like this:
As you would notice your newlly created image would not have a name nor a tag, so in order tag your image run:
docker tag IMAGE_ID YOUR_TAG
Now if you list your images you would see something like this:
You could also push your new image to DockerHub. for that you would need a docker hub account first:
Then push your image to the DockerHub:
docker push your-docker-user/name-of-image-here
The output would look something like this:
After that you should be able to see your docker image in your docker hub account, in my case it would be here:
Modifying images with Dockerfile
We will go the Dockerfile a bit more in depth in the next blog post, for this demo we will only use a simple Dockerfile just as an example:
Create a file called Dockerfile and add the following content:
RUN apk update
To build the image run:
docker image build -t alpine-updated:v0.1 .
Then you could again list your image and push the new image to the DockerHub.
That is pretty much it! Now you know how to pull, modify and push images!
We are ready to move to Part 4 of the Introduction to Docker blog post series:
Let me know if you have any questions!