Posted on Leave a comment

Docker Basics – Install, First Run and Basic Commands

Installing on Debian based Systems

Docker is incredibly simple to install and get started. We will be installing the repository version on Linux Mint. This installation should work on any Debian system.

To install, simply open a shell and use the following apt commands to install the software.

For secure communications you may need to perform one or more of the following . If your user does not have root level permissions you will need to precede these command with sudo or perform an su root.

apt install apt-transport-https
apt install ca-certificates 
apt install curl 
apt install gnupg-agent 
apt install software-properties-common 

You can run all the commands and evaluate each, I only needed the last three. They are all related to the installation process and not the actual operation of docker.

Start by adding the docker repository keys to apt. If you are root or running as root with su remove the sudo after the pipe in the command.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -

Then check that you have the right key

apt-key fingerprint 0EBFCD88

Now add the repository:

add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" 

Use this for Ubuntu based:

add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Update apt and install Docker

apt update
apt upgrade
apt install docker-ce

That all there is to it. You know have Docker installed

You can also install using the administration tools like Synaptic or the Software Manager.

As a side note you can learn quite a bit about Docker from the command itself by simply typing the command alone in the console. You will be supplied with Usage, Options and Commands like any other bash command.

Your First Container

Adding an Image

Docker deals in images and runs them as containers. to get an image you use the docker pull command. I will be getting and running WordPress.

docker pull wordpress

I used the WordPress image for this example but there are thousands so you can substitute one of your choice. You can browse popular images here: https://hub.docker.com/search?q=&type=image

Listing Images

docker images

Running a Container ( Initializing the Container )

To run a container we use the “run” command. A container is run from an image and is given a name/id the first time it is run. We will use the name option to help with managing this container. The container is only “run” once. To use a container after the initial run we can utilize the start and stop commands.

From the official image for WordPress from the Docker Hub https://hub.docker.com/_/wordpress/ we can use this simple variant:

docker run --name my-wordpress -p 8080:80 -d wordpress

The -p maps a port, in my case I will be re-directing the port 80 configured within the image to listen on port 8080 on the container.

That’s it, you now have a running instance of WordPress. You will have to go through the setup and the database portion, which is inevitable. Notice how you did not have to install Apache, install PHP, install/configure Apache PHP etc., Configuring WordPress to a fully working state is for another post, but the idea is clear.

Managing Containers

Start and Stop a Container

docker start container-name/id
docker stop container-name/id

List Running Containers

Many of the docker commands are bash-like. So listing running containers is similar to listing running processes.

docker ps

List All Containers

Listing all containers introduce the idea of nested commands in Docker. The container command is followed by the ls “sub”-command.

docker container ls

Remove a Container

docker rm container-name/id

Working with a Container.

Run BASH commands in a container

docker exec -it some-wordpress bash

Copy Files to/from an existing Container

TO: 

docker cp filename container-name:filename

FROM:

docker cp container-name:filename filename

So that’s it, those are the basic operations of Docker. Had I known it was so straight forward I would have started years ago.

Examples with Ouput:

Listing Images

randall@randall2:~$ docker images
 REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
 wordpress           latest              f790ed010456        4 days ago          540MB
 wordpress                         a9f43b7c47db        5 weeks ago         539MB
 huginn/huginn       latest              be71ee4ce404        5 weeks ago         984MB
 nodered/node-red    latest              51ca17a75fc0        5 weeks ago         433MB
 mysql               latest              d435eee2caa5        2 months ago        456MB
randall@randall2:~$ 

Running a Container from an Image

randall@randall2:~$ docker run --name some-wordpress -p 8080:80 -d wordpress
 f3f6b0d41956163c0b1341dc234023b56eb3f9f8218c5d891fda8b970f70654ddocker 

randall@randall2:~$ docker run -it -p 3308:3306 --name new-mysql -e MYSQL_ROOT_PASSWORD='somepassword' -d mysql
 974a677faa2a7694f913acc925687d8bac68e0ac53ef50f8e54aad611af0f312

Listing Running Containers

randall@randall2:~$ docker ps
CONTAINER ID    IMAGE       COMMAND                  CREATED               STATUS              PORTS                               NAMES
40e9fb4b49d8    mysql       "docker-entrypoint.s…"   20 hours ago        Up About a minute   33060/tcp, 0.0.0.0:3307->3306/tcp   mcp-mysql
91096c6b8dcb    wordpress   "docker-entrypoint.s…"   23 hours ago       Up About a minute   0.0.0.0:8080->80/tcp                some-wordpress
 

Listing all Containers

randall@randall2:docker container ls

The output will be similar to the above

Start and Stop a Container

randall@randall2:~$ docker start some-wordpress
some-wordpress

Getting BASH inside a container

randall@randall2:~$ docker exec -it mcp-mysql bash
root@40e9fb4b49d8:/#

Copy files to and from a container

randall@randall2:~$docker cp nodered:/data/settings.js .
There will be no output for a successful copy
docker cp settings.js nodered:/data/settings.js
There will be no output for a successful copy

Removing a container

randall@randall2:~$ docker rm some-wordpress some-wordpress
Leave a Reply

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