Docker Basics

Docker Basics

This post isn't going to be about everything Docker, it is just about the basics of using Docker in a Linux operating system.  In Fedora 25, I used the following commands.

Getting Started with Docker

First, you need to get Docker software installed on your Linux system using your package manager.
In Fedora, I executed the following command to install docker:
dnf install docker
or in SUSE:
zypper install docker
or in Debian/Ubuntu:
apt-get install docker

Next, you need to start and enable the docker service.

systemctl start docker
systemctl enable docker

To verify that your installation is successful execute:

docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:

For more examples and ideas, visit:

Exploring Docker

If all went well, then you can begin to search for images that you might want to use like "minimal", "centos", "suse", "ubuntu" or "mariadb".

docker search minimal

If you execute the docker command without any sub-commands, then it will display a summary of the usage of the docker command:

Usage: docker [OPTIONS] COMMAND [arg...]
       docker [ --help | -v | --version ]

A self-sufficient runtime for containers.


  --config=~/.docker              Location of client config files
  -D, --debug                     Enable debug mode
  -H, --host=[]                   Daemon socket(s) to connect to
  -h, --help                      Print usage
  -l, --log-level=info            Set the logging level
  --tls                           Use TLS; implied by --tlsverify
  --tlscacert=~/.docker/ca.pem    Trust certs signed only by this CA
  --tlscert=~/.docker/cert.pem    Path to TLS certificate file
  --tlskey=~/.docker/key.pem      Path to TLS key file
  --tlsverify                     Use TLS and verify the remote
  -v, --version                   Print version information and quit

    attach    Attach to a running container
    build     Build an image from a Dockerfile
    commit    Create a new image from a container's changes
    cp        Copy files/folders between a container and the local filesystem
    create    Create a new container
    diff      Inspect changes on a container's filesystem
    events    Get real time events from the server
    exec      Run a command in a running container
    export    Export a container's filesystem as a tar archive
    history   Show the history of an image
    images    List images
    import    Import the contents from a tarball to create a filesystem image
    info      Display system-wide information
    inspect   Return low-level information on a container, image or task
    kill      Kill one or more running containers
    load      Load an image from a tar archive or STDIN
    login     Log in to a Docker registry.
    logout    Log out from a Docker registry.
    logs      Fetch the logs of a container
    network   Manage Docker networks
    node      Manage Docker Swarm nodes
    pause     Pause all processes within one or more containers
    port      List port mappings or a specific mapping for the container
    ps        List containers
    pull      Pull an image or a repository from a registry
    push      Push an image or a repository to a registry
    rename    Rename a container
    restart   Restart a container
    rm        Remove one or more containers
    rmi       Remove one or more images
    run       Run a command in a new container
    save      Save one or more images to a tar archive (streamed to STDOUT by default)
    search    Search the Docker Hub for images
    service   Manage Docker services
    start     Start one or more stopped containers
    stats     Display a live stream of container(s) resource usage statistics
    stop      Stop one or more running containers
    swarm     Manage Docker Swarm
    tag       Tag an image into a repository
    top       Display the running processes of a container
    unpause   Unpause all processes within one or more containers
    update    Update configuration of one or more containers
    version   Show the Docker version information
    volume    Manage Docker volumes
    wait      Block until a container stops, then print its exit code

Run 'docker COMMAND --help' for more information on a command.

For the next recommended test, try out the ubuntu image.  To start the bash shell inside the ubuntu container, execute:

docker run -it ubuntu bash


To see the this container running from another terminal execute:

docker ps

CONTAINER ID    IMAGE            COMMAND   CREATED            STATUS              PORTS        NAMES
bb21045eda79        ubuntu              "bash"              8 seconds ago       Up 5 seconds                            hopeful_fermi

The name shown "hopeful_fermi" was automatically generated. Replace this name with the name automatically generated by your system in the command below.

To stop this container  from another terminal execute:

docker stop hopeful_fermi

To start a container based upon the ubuntu image with a specific name like "zesty" execute:

docker run -it --name "zesty" ubuntu bash

To see the this container running from another terminal execute:

docker ps

CONTAINER ID    IMAGE            COMMAND   CREATED            STATUS              PORTS        NAMES

2658f64e637a        ubuntu              "bash"              37 seconds ago      Up 35 seconds                          zesty

Saving Docker Container Changes

What if you've customized your docker container, and you want to launch the customized changes the next time you use it? One technique that can be used with the docker is the commit sub-command to save the changes to a new container. 

Start running a docker container for the mariadb database mapping with -p local:remote and -e ENVIRONMENT_VARIABLE=value:

docker run  -p 3306:3306 -e MYSQL_ROOT_PASSWORD='secret' mariadb

The name that you see for your container may not be what you want it to be when you use the command to view running processes. You can use the name sub-command or pass a --name option when beginning to run the container.

docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
0675838efe90             mariadb              "docker-entrypoint.sh"   46 minutes ago      Up 46 minutes>3306/tcp   wright-mariadb

You may want to rename the container based upon the image like:

docker rename 0675838efe90 colorsdb

Then, you can save the changes to your container with a docker commit command:

docker commit colorsdb

For more details, you can look at the help available for the commit command.

docker commit --help

Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:  -a, --author string    Author (e.g., "John Hannibal Smith ")  -c, --change value     Apply Dockerfile instruction to the created image (default [])      --help             Print usage  -m, --message string   Commit message  -p, --pause            Pause container during commit (default true)

To stop running the container, you can now use:

docker stop colorsdb

To start the container with the changes that have been committed to it after it has been stopped, you can use:

docker start colorsdb

Using Docker Containers

So now what? You can download and run an image and create a container or execute commands inside of it. You might be wondering like me, how do you use this container? 

In order to access to use some containers, you can access these containers through ports they expose to the host system or to other containers.

To connect to the container that has exposed port 3306 on the localhost to map to 3306 on the remote the machine, you could execute:

mysql -h localhost -P 3306 --protocol=tcp -p

No comments:

About Me - WrightRocket

My photo

I've worked with computers for over 30 years, programming, administering, using and building them from scratch.

I'm an instructor for technical computer courses, an editor and developer of training manuals, and an Android developer.