Wednesday, August 29, 2018

Introduction to Docker - Day 3

On Day 3, we will learn more about running docker app and various options. 

Running Docker Image

To run a docker image, the basic syntax is as follows:

docker run <image_name> 

Basic syntax is only able to run hello-world properly. For other apps, we need additional options.

To run a docker image with optional container name use the syntax:

docker run --name <container_name> <image_name>


docker run --name someName hello-world

Run Interactive Docker App

To run an interactive app we use the following command:

docker run -i -t --name <container_name> <image_name> <command_in_app>
  • -i is for interactive session
  • -t is to create a tty for user interaction
  • Usually we use -it
  • we can append additional command to be executed in the app. The command should be appended after the image name


docker run -it --name myubuntu ubuntu

We perform any task such just like a virtual machine. Type exit to end the session. We can remove the container using the custom container name that we supplied.

Example 2:

docker run -it --name python1 python:3.7-slim

Once we command is executed, we are brought into python shell. If we exit the shell, the container will be terminated and exit.

We can also run the above command without going into Python. We do that using alternate command as shown below:

docker run -it --name python2 python:3.7-slim /bin/bash

The space after the image name is reversed for execution command. We append the command to run bash shell instead of Python. We can enter Python from the bash shell and once we exited from Python shell, the system will return us to bash shell.

Run Server App

We can also run server app, however, the options are different. The syntax of the command is as follows:

docker run -d --name <container_name> -p <local_port>:<server_port> <image_name>

  • -d is to tell the container to run but detached from the terminal shell. This way we can still use the terminal for other purpose. 
  • -p is to map network port  from our Mac OS with the Linux system.
  • Please note that when we use -d, the server app will be running in the background, we need to use ps command to check the process and kill it when we need to stop the server.
For this example, we will run nginx webserver. Use the command below:

docker run -d -p 80:80 --name webserver nginx

The screen will return to prompt. To check for the process, we use the command docker ps.

docker ps

To test the webserver, use the address https://localhost in a web browser. You should see the following message from the web browser.

To terminate the server use the following command

docker stop webserver

If we check the docker process, we should return no process.


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.