Skip to main content

General Docker Information

====================================================================================

To preface this page; I personally recommend trying out Portainer, which is essentially a web GUI for Docker - it's great. Portainer even lives in a docker container itself.

Regarding images and container management; the contents of this page are great if you're working with a small number of containers that are being installed/setup as a one-off then this is all fine, however, if you're launching containers for applications that need to be updated, changed/customised etc then you're probably best off taking a look at the Docker Compose page.

====================================================================================

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

-----------------------------------------------------------------------------------------------------------------------------

Managing Images

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

https://hub.docker.com/ 

To 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 container-nameimagename

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

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 market place;marketplace;

root@test:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete 
Digest: sha256:91fb4b041da273d5a3273b6d587d62d518300a6ad268b28628f74997b93171b2
Status: Downloaded newer image for hello-world:latest

Hello from Docker!

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.







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