Put user on docker group

If you don’t want to type “sudo” on every docker instruction, check if the “docker” group exists on your system :

cat /etc/group | grep docker

If the group does not exists, create it :

sudo groupadd docker

And put your user on this group :

sudo gpasswd -a <username> docker

You now have to restart docker and refresh group informations :

sudo service docker restart
newgrp docker

Images Download

Docker.io give you a lot of various images. You can browse it on the Docker hub.

In this article, we will use Ubuntu 14.04.

Launch this command to download the image :

docker pull ubuntu:trusty

The image is light, this is the benefit of Docker. The download can take many minutes depending on your connection.

When the download is finished, type this command :

docker images

You will show a result like this :

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu trusty a5a467fddcb8 2 days ago 187.9 MB

If you want to delete an image, type this :

docker rmi <IMAGE_ID>

You can use the TAB key to autocomplete the ID

Instead of use IMAGE ID you can write <REPOSITORY>:<TAG> like “ubuntu:trusty”

We will install ubuntu update and save it on a new docker image throughout 2 methods.

Image creation with shell

Now, we can run our image and use its shell :

docker run -it <IMAGE_ID> bash

– Parameter -i allow the stdin/stdout access
– Parameter -t allow tty access

You can display running containers with the command :

docker ps

You must run it on your system’s shell and not on the container shell !
Here is the return of the ps commande for me :

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ebbe6ea61aa ubuntu:trusty bash 4 minutes ago Up 4 minutes jolly_yonath

Now, we can run some shell command in our container. The user is “root” and it’s not a problem because we are on a virtual and closed context.

We want to install ubuntu updates, so launch :

apt-get update

When it’s finished, type CTRL + D to exit container.
If you type docker ps command, the list will be empty.

In order to show the closed container, add a paramater :

docker ps -a

Our container is here :

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ebbe6ea61aa ubuntu:trusty bash 8 minutes ago Exited (0) About a minute ago jolly_yonath

You can run many operations on containers, here is a list of most used :

docker stop <CONTAINER_ID> # Stop container
docker start <CONTAINER_ID> # Start container
docker restart <CONTAINER_ID> # Stop/Start container
docker kill <CONTAINER_ID> # Kill container
docker rm <CONTAINER_ID> # Delete container

Instead of use CONTAINER_ID you can use NAMES field, auto-genarated in our case. If you want to give a custom name, launch :
docker run -it --name test_nom ubuntu:trusty
You can use this name in yours commands, it must be unique else Docker will throw an error.

We can now save our modifications on a new image :

docker commit <CONTAINER_ID> <NEW_IMAGE_NAME>

I named it “test_shell” and I see it with docker images command :

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
test_shell latest 80d3effbbe3e 8 seconds ago 209.2 MB
ubuntu trusty a5a467fddcb8 2 days ago 187.9 MB

Image creation with Dockerfile

You can automatically do what we did before with a Dockerfile. In our example, we launch only one command, but you can do more complexe images with a lot of command on your Dockerfile. At this time, let’s make our update.

I decided to create a Docker folder on my personal folder, and a test folder to put my file :

mkdir ~/Docker/test
vi ~/Docker/test/Dockerfile

The content file is :

# We use ubuntu image
FROM ubuntu:trusty
 
# And make updates
RUN apt-get update

We have two instructions :
– FROM, used only one time on a Dockerfile, and specify which image to use
– RUN, followed by a shell command. If you have many commands to run, each of them must be preceded by this instruction.

Save the file, and go inside the folder containing your Dockerfile

cd ~/Dockerfile/test

And launch the build command :

docker build -t test_dockerfile .

You will see the Dockerfile launch your instructions step by step.
When the build is finished, type docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
test_dockerfile latest 4d9f3e8836ab About a minute ago 209.2 MB
test_shell latest 80d3effbbe3e 20 minutes ago 209.2 MB
ubuntu trusty a5a467fddcb8 2 days ago 187.9 MB

Our two custom images have the same size because they have the same content.

Carry your Docker image

With Docker, you can use created images ont every system with the package installed.
We will create an archive for the “test_dockerfile” image to carry it easily :

docker save -o test_dockerfile.tar test_dockerfile:latest

The file is created on the folder you are, you can also put an other path.

On the receiving machine, you can load the image :

docker load --input test_dockerfile.tar

With a docker images, there is no REPOSITORY and no TAG, so we will name it :

docker tag <IMAGE_ID> test_dockerfile:latest

Ou instructions were really simple, but it’s important to understand the Docker behaviour.

Getting started with Docker
Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies to ensure that we give you the best experience on our website.
Ok