DOCKER : RUN

Avishek Roy
teckdevops
Published in
6 min readMay 11, 2019

A quick guide to docker run command i.e. its usage , various options and examples.

Docker Run

Prerequisite

docker should be installed, up and running.

IF docker != installed

THEN

Follow the link for docker installation on centos/rhel machines.

ELSE

CONTINUE

Overview

docker run in-short is the combination of docker create & start command i.e. it creates a container and then starts it too. Below is docker syntax and a few useful options/tags available.

Syntax

docker run [OPTIONS] image [COMMANDS] [ARGS..]

Options

docker run \
--name container-name\ # assign a name to d container
--volume volume\ # mount a host volume
--network my-network\ # connect to a specified network
--privileged \ # extended privileges
--detach , -d \ # run a container in background
--tty, -t \ # pseudo tty allocation
--rm \ # remove a container on exit
--hostname, -h \ # container host-name
--env, -e \ # environment variables
--publish, -p\ # host --> container port mapping
IMAGE \ # image to run a container
[COMMANDS] \ # optional commands
[ARGS] # command arguments

Examples

Running a Nginx container

$ docker run --publish 8888:80 --name webserver --detach nginx

The last command will spin up a container in the background (detach) i.e. named as ‘webserver’ using the Nginx image and maps port 80(container) to 8888(host). docker will first look for concerning image locally and if not found then we will go for a search on the docker hub.

docker run — nginx

Now try to access the Nginx URL on your favorite browser, quick & easy, isn’t it!

GUI

192.168.99.100 → docker host/IP

8888 → Host Port

If you want to check for docker host IP or other environment details, use docker-machine env command.

docker-machine env

2. Ansible setup via docker.

We will go a bit further in our second example where we first create a docker network and then a few containers(in the same network) and finally try to find out if we can connect a container from the other one. Also, we extend our example to set up and install ansible and make a container as a master node and the rest as slave nodes.

A. Create a network

First, create a network i.e. to be used by the following container and our ansible setup.

teckdevOps$ docker network create dockernetworkteckdevOps$ docker network ls
NETWORK ID NAME DRIVER SCOPE
5e80ddf5fd77 bridge bridge local
314c26993776 dockernetwork bridge local
69092bce12cd host host local
3a71e8205fec my_dns_check bridge local
54aaa7291609 none null local
teckdevOps$
docker network

Our network has now been created and by default its using driver as bridge though it can be customized with — driver option.

B. Create Master and Slave machines.

We going to use the centos image for our master & slave containers. Run below commands to run 3 containers i.e. 1 master node and 2 slave nodes for our ansible setup.

teckdevOps$ docker run -d -t --name master_ansible --cap-add=NET_ADMIN -h controlnode --privileged=true --network dockernetwork centos /usr/sbin/init

teckdevOps$ docker run -d -t --name app01 --cap-add=NET_ADMIN -h app01 --privileged=true --network dockernetwork centos /usr/sbin/init

teckdevOps$ docker run -d -t --name app02 --cap-add=NET_ADMIN -h app02 --privileged=true --network dockernetwork centos /usr/sbin/init
docker — master/slave setup

- -cap-add → Add Capabilities to a container

NET_ADMIN → Perform various network-related operations.

Now, as our containers are up and ready so, let’s jump into the master container and roll-on ansible installation. docker exec command is generally used to log in onto a container to run commands or perform various actions.

teckdevOps$ docker exec -it master_ansible /bin/bash

Login success! now let’s try to ping other nodes(app) from the control node and it should go through well and be validated via a success ping response.

docker exec

Now let’s quickly install ansible on our master node and also other packages on our container as the same is required for our machine to go onto a full-fledged ride.

C. Install packages on containers

i) control node → ansible & ssh

install ansible
install ssh

ii) app nodes → ssh

ssh on app01
ssh on app02

D. start ssh daemon (on all containers)

start ssh daemon

Caution : I got an issue while start of sshd service and here is the link to an another blog article that is to be followed to conquer the errors and having the troubleshooting steps.

Now our setup is almost ready and the only left out part is ansible ssh key based setup that is required for passwordless authentication across our machines i.e. from the control node to app nodes ( control → app01/app02).

E. Ansible Setup(password less authentication)

First, add a new user i.e. ansible (not recommended to use a root account).

ansible user

Secondly, setup ssh keys (generate and copy)

generate keys
copy app01
copy app02

Now finally it’s time to test our setup, yeahhhh!

Please remember to setup ansible hosts file entries as below and once done we good to go with UAT of our master-slave ansible setup.

ansible hosts entries
ansible test — ping module
ansible command module

Success, UAT passed!! Ready for Go-live!! :-)

We achieved our mission and our ansible setup using docker containers is up and ready and functioning well.

Command Set

teckdevOps$ yum install ansible
teckdevOps$
yum install openssh-server openssh-clients
teckdevOps$ docker exec -it master_ansible /bin/bash
teckdevOps$ systemctl start sshd
teckdevOps$
systemctl status sshd
teckdevOps$ ssh-keygen -t rsa
teckdevOps$ ssh-copy-id ansible@app01
teckdevOps$ ansible -m ping all
teckdevOps$ ansible -m command -a "hostname" all

Epilogue

We have seen the power of docker and docker run and its an extensive and quite useful tool and is light, quick and fast too, especially for orchestrating tons of machines for the test or prod setups.

Hope you find this article useful, please comment/feedback for any sort of suggestions or concerns till then Happy Reading! Adios!

— A blog by teckdevOps

--

--