Skip to main content

Basic Usage


Installation

https://docs.docker.com/engine/install/ 

1. Configure Docker repo (See above link)

2. Install Docker CE (Community Edition)

apt install docker.io

Alternatively, there is a script to perform an automated installation of Docker; Again, see above link.


Commands & Usage

Managing Containers

List Docker processes/containers (running and stopped)

docker ps -a

Start a container

docker start containername/ID

stop a container

docker stop containername/ID

Delete a container

docker rm containername/ID

Check container performance metrics

  docker stats containername/ID

Inspect Container Configuration

shows configuration details and current state.

docker inspect containername/ID

View container logs

docker logs containername/ID

Managing Images

Docker has their own 'marketplace' of sorts for docker images:

https://hub.docker.com/ 

Install a new docker image

This won't start a container based on the image. Instead, the image is just added to the locally stored images.

docker pull imagename

By default, docker will only search it's official repository for available images. If you're wanting to pull an image that's been saved into a user's docker repository, then you'll need to specify the username;
docker pull username/imagename

Image Tags

By default, when you run docker pull to download an image, it will automatically download the latest available version from the docker image hub. However, lets say you don't want the latest version, and have a direct requirement for an older version. In this scenario, we can make use of tags.

Docker image repositories will often contain older versions, you can check the repo page for the available versions, as an example, see the Redis page:

https://hub.docker.com/_/redis 

You can use the version tag to specify the required version:

docker pull imagename:tag

example;

docker pull redis:6.2
List installed Docker images
docker images
Search locally installed images, and search Docker repository for available images
docker search name
Remove a Docker image
docker rmi imagename

Launching Containers and issuing commands

To launch a new container, we can use the docker run command. The docker run command will search for locally installed images, and it can also search the docker marketplace if no local image is found.

Using docker run without the -d flag will attach your session to the containers STDOUT. See HERE for more details.

A basic example of using docker run to pull an image (basic hello-world container) from the marketplace;

Using the docker run command, you can also issue a command for the container to process. For example;

docker run container command

You can also issue commands to existing containers using the exec option;

docker exec containername command

Example;

In this example, I've pulled the ubuntu image from docker marketplace and initiated a container. I've issued the command 'sleep 100' to this container, essentially telling it to wait for 100 seconds':

root@test:~# docker run ubuntu sleep 100

I then issued a command to that running ubuntu container using exec;

root@test:~# docker exec elastic_bell cat /etc/hosts
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.17.0.2	f81a3509090e

There are lots and lots of options available for the docker run command

-d detach from container.
-i interactive mode
-t launch container with terminal
-v /path/on/host:/path/on/container Specify persistent data storage
-p 80:5000 Port mapping
-u username Specify user to run the container
--entrypoint command Manually set an ENTRYPOINT command for the container to start with, overriding that set in the image configuration.
--name name Give the container a name.

--link containername:containerhost

DEPRACATED

Link 2 containers together. When using --link to run a container, an entry will be added to the /etc/hosts file on your new container, meaning that it's able to reach the container specified in the link parameter.

Attaching/Detaching to/from a container

By default, when using the docker run command your session will be attached to the new container.

Detach from a container (container continues running):

ctrl+p ctrl+x

To attach to STDOUT of a docker container:

docker attach containername

To connect to a docker container CLI:

docker exec -it containername bash