DEV Community

Cover image for How to use Docker commands
Steadylearner
Steadylearner

Posted on • Edited on • Originally published at steadylearner.com

How to use Docker commands

In this post, we'll learn how to use Docker commands. We will make a web app inside a Docker container and turn it into a Docker image. We'll also learn how to upload it to Docker Hub.

You can find Portuguese version of this post here.

Prerequisite

  1. How to install Docker
  2. Docker Website, Docker Curriculum
  3. How to deploy a container with Docker, Docker lifecycle

First, you must install Docker, if you don't have it yet. Type $docker on your machine and you will be shown how to proceed with the installation. Or just search how to install it on your browser.

This post is a summary of Docker Website, Docker Curriculum etc. I hope you read them first, but you don't have to spend a lot of time with them. We'll learn how to deploy a web app and microservices to AWS with other Steadylearner Blog posts.

Table of Contents

  1. Confirm installation with Nginx
  2. Set up your development environment with Docker
  3. How to move your local files and folders to docker containers
  4. How to use the web framework with docker containers
  5. How to modify the network ports of docker images
  6. Docker Images and Containers
  7. How to upload your Docker images to Docker Hub
  8. Conclusion

1. Confirm installation with Nginx

I hope you can install Docker. Before we learn how each Docker command works, let's test whether or not these can show some results on your machine.

Use them in your CLI.

$docker search nginx
$docker pull nginx
$docker run --name nginx-webserver -p 80:80 nginx
Enter fullscreen mode Exit fullscreen mode

Then, access localhost. This will be shown in your browser:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.
Enter fullscreen mode Exit fullscreen mode

Realize that you only need a few commands to use Docker. You could also start a Docker container with a specific name and execute bash comamnds with these.

$docker run --name nginx-webserver -p 80:80 -d nginx
$docker exec -it CONTAINER_ID bash
Enter fullscreen mode Exit fullscreen mode

2. Set up your development environment with Docker

In this section, we'll learn how to set up a default Docker image with Ubuntu. It will be possible to reuse this image later. If you use another OS, please, use that instead and refer to this part.

Start with pull commands to download the official ubuntu image from Docker Hub. If Docker Hub is something new for you, you can compare it to GitHub because of its repositories.

$docker pull ubuntu
Enter fullscreen mode Exit fullscreen mode

Now, make a container in your machine. To download minimal softwares, use sh or bash commands in it with this:

$docker run -it ubuntu sh
Enter fullscreen mode Exit fullscreen mode

Start by installing CURL to download other programs.

$apt-get update
$apt-get install curl
$curl https://www.steadylearner.com
Enter fullscreen mode Exit fullscreen mode

If you are out of the container, restart it with this:

$docker exec -it CONTAINER_ID bash
Enter fullscreen mode Exit fullscreen mode

You can find the CONTAINER_ID with docker ps -a. This is the command you will use often and it will show you some useful metadata of Docker containers.

We'll make a simple Node "Hello, World" web app example in this post. Let's start by configuring the Node development environment. Follow these steps if you want to use the same project as this post.

Also, you can use $docker run -d steadylearner/ubuntu_node instead.

You should be inside your docker container to use them.

Node, NPM, Yarn

curl -sL https://deb.nodesource.com/setup_12.x | bash
Enter fullscreen mode Exit fullscreen mode

This will be displayed:

## Run `sudo apt-get install -y nodejs` to install Node.js 12.x and npm
## You may also need development tools to build native addons:
    sudo apt-get install gcc g++ make
## To install the Yarn package manager, run:
    curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
    sudo apt-get update && sudo apt-get install yarn
Enter fullscreen mode Exit fullscreen mode

You should use commands without sudo.

apt-get install gcc g++ make
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

apt-get update && apt-get install yarn
Enter fullscreen mode Exit fullscreen mode

Follow those commmands and install them all.

Test Node work with this:

$node
$console.log("Hello from www.steadylearner.com");
Enter fullscreen mode Exit fullscreen mode

Vim

Use this command with --assume-yes or -y to skip relevant install questions.

apt install --assume-yes vim
Enter fullscreen mode Exit fullscreen mode

Vim text editor will be installed. Now, use this command to use it:

$vim hello.js
Enter fullscreen mode Exit fullscreen mode

Edit your hello.js file with this and :wq to save and quit from the Vim.

// hello.js
console.log("Hello from www.steadylearner.com");
Enter fullscreen mode Exit fullscreen mode

Use this command to verify that Node is installed correctly:

$node hello.js
// Hello from www.steadylearner.com
Enter fullscreen mode Exit fullscreen mode

Git

$apt-get --assume-yes git-core
Enter fullscreen mode Exit fullscreen mode

This will install Git and verify that it is installed and its version:

$git --version
Enter fullscreen mode Exit fullscreen mode

Then, get your GitHub username and email from your local machine:

$git config --get user.name
$git config --get user.email
Enter fullscreen mode Exit fullscreen mode

Use them in the Docker container to operate Git inside:

$git config --global user.name yourname
$git config --global user.name youremail
Enter fullscreen mode Exit fullscreen mode

Make use of the same command (--get) before checking them in your Docker container.

Test Git clone work to download files from your previous GitHub repositories. For example, clone steadylearner/docker-examples repository with this:

$git clone https://github.com/steadylearner/docker-examples.git
Enter fullscreen mode Exit fullscreen mode

I hope you can install everything you think is necessary in your Docker container.

You can skip this Yarn relevant part and use default npm commands instead. Otherwise, read this post for more information.

Verify the Yarn version first in it.

$yarn -v
Enter fullscreen mode Exit fullscreen mode

It will show the version of your Yarn.

Next, use these commands to use Node project:

$cd /home
$mkdir node && cd node
$yarn init
$yarn add chalk
Enter fullscreen mode Exit fullscreen mode

Test NPM or Yarn work with NPM modules with these:

// Start with $node in your console and use each command.

const chalk = require("chalk");
const blue = chalk.blue;
const hello = blue("Hello from www.steadylearner.com");
console.log(hello);
Enter fullscreen mode Exit fullscreen mode

It should have shown Hello from www.steadylearner.com message in your console.

We verified that NPM packages work in your docker container with this.

If you want, make alias for this directory also similar to this.

$vim ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Type this and :wq to save and quit.

alias work="cd /home/node"
Enter fullscreen mode Exit fullscreen mode

Use $source ~/.bashrc and you can use your node project with $work whenever you want. Also, you can define WORKDIR later with Dockerfile or docker-compsose.yml for the same purpose.

There will be many Docker containers in your machine. Use these commands to remove unnecessary ones:

1. List and remove previous Docker containers.

$docker ps -a
Enter fullscreen mode Exit fullscreen mode

The list of instances that you ran before will be shown.

2. Remove what you don't need.

$docker stop containerid
$docker rm containerid
Enter fullscreen mode Exit fullscreen mode

or

$docker rm containerid -f
Enter fullscreen mode Exit fullscreen mode

3. How to move your local files and folders to docker containers

We can use Git commands to download files from the GitHub into your containers. Moreover, you can use Docker commands to move local files and folders to your Docker containers and vice versa.

Refer to these examples or docker cp --help.

1. Files

$docker cp from_localhost.txt containerid:/from_localhost.txt
$docker cp containerid:/from_docker from_docker.txt
Enter fullscreen mode Exit fullscreen mode

2. Folders

$docker cp from_localhost containerid:/from_localhost
$docker cp containerid:/from_localhost from_localhost
Enter fullscreen mode Exit fullscreen mode

4. How to use web frameworks with docker containers

We installed Node relevant softwares for this part. If you use web frameworks from other languages, please, refer only to the workflow of this section.

Express

Install the dependencies we'll use inside the docker container with this:

$yarn add express chalk
Enter fullscreen mode Exit fullscreen mode

Next, we'll build "Hello, World!" app with the JavaScript code below.

// server.js
const express = require('express')
const chalk = require("chalk");

const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello, World!'))

const blue = chalk.blue
const target = blue(`http://localhost:${port}`)

app.listen(port, () => console.log(`Express Server ready at ${target}`))
Enter fullscreen mode Exit fullscreen mode

Then, $node server.js will display this message:

[Express] Server ready at http://localhost:3000
Enter fullscreen mode Exit fullscreen mode

But $curl http://localhost:3000 or visiting it in your browser won't work yet.

Each container has its own IP to network. We should inspect the docker container with $docker inspect CONTAINER_ID > inspect.txt to extract the information from it.

You can find its local IP at the end of inspect.txt and it will be similar to 172.17.0.2. Save your time doing getIP.js and $node getIP.js.

const fs = require('fs')

const filename = "inspect.txt";

fs.readFile(filename, 'utf8', function(err, data) {
  if (err) throw err;

  // console.log(`Read ${filename}`);
  const dataObject = JSON.parse(data);

  // console.log(payload);
  // console.log(typeof payload);

  const ip = dataObject[0].NetworkSettings.IPAddress;
  console.log(`IP is ${ip}`);
});
Enter fullscreen mode Exit fullscreen mode

You can use the docker inspect command as well.

Test the IP with $curl http://172.17.0.2:3000/ or verify it with your browser.

If you can see this message, realize that you can actually develop web apps on your local machine with Docker.

Hello, World!
Enter fullscreen mode Exit fullscreen mode

5. How to modify the network ports of docker images

In the previous part, we had to find the network port for the web framework to visit it. Alternatively, you can start with your custom port.

$docker run -it --name ubuntu_node -p 80:80 ubuntu
Enter fullscreen mode Exit fullscreen mode

You can also use it with -d to make the container run in background..

docker run -d --name ubuntu_node -p 80:80 ubuntu:latest
Enter fullscreen mode Exit fullscreen mode

Refer to this to find out what happens here better.

'By default, the port on the host (container) is mapped to 0.0.0.0, which means all IP addresses. You can specify a particular IP when defining the port mapping, for example, -p 127.0.0.1:80:80'

6. Docker Images and Containers

You may find yourself confused by the difference between Docker container and image. Just think of the images as classes and containers as instances that you are using on your machine. You can:

  1. Pull or run (pull and start) images and it will make docker containers from them on your local machine.

  2. Edit files in your containers with $docker exec -it containername bash.

  3. Make images from the containers with $docker commit containername YourDockerHub/image && docker push account/image.

Feel free to start with Dockerfile instead of 1. and 2. and commit your docker images as well. We'll learn that in another Steadylearner Blog with Elastic Beanstalk.

7. How to upload your Docker images to Docker Hub

Now, we are going to learn how to create a repository first at Docker Hub with the example we made.

First, login with this command:

$docker login
Enter fullscreen mode Exit fullscreen mode

Then, use $docker commit.

$docker commit ubuntu_node
Enter fullscreen mode Exit fullscreen mode

Next, verify the image made from the ubuntu_node container using this:

$docker images
Enter fullscreen mode Exit fullscreen mode

Give it a tag (name).

$docker tag imageid steadylearner/ubuntu_node
Enter fullscreen mode Exit fullscreen mode

You can execute this command instead:

$docker commit ubuntu_node steadylearner/ubuntu_node
Enter fullscreen mode Exit fullscreen mode

Now, you can push your docker image to Docker Hub with this:

$docker push steadylearner/ubuntu_node // yourusername/image
Enter fullscreen mode Exit fullscreen mode

Wait for uploading process to complete and use this:

$docker run -it steadylearner/ubuntu_node bash
Enter fullscreen mode Exit fullscreen mode

If you want to edit it, just follow the same steps we used before.

Restart the containers with this, if they stop:

$docker restart containerid
$docker exec -it containerid bash
Enter fullscreen mode Exit fullscreen mode

To remove container made from steadylearner/ubuntu_node image or yours, you can use this:

$docker stop ubuntu_node
$docker container rm ubuntu_node
$docker image rm ubuntu
Enter fullscreen mode Exit fullscreen mode

If you want to rename the container, use this:

$docker container rename randomname ubuntu_node
Enter fullscreen mode Exit fullscreen mode

Use yours instead of ubuntu_node or steadylearner/ubuntu_node.

If you modify the project, use the commands similar to this:

$docker commit ubuntu_node steadylearner/ubuntu_node
Enter fullscreen mode Exit fullscreen mode

Or with a commit message.

$docker commit --message "Test message and will be similar to github -m option" ubuntu_node steadylearner/ubuntu_node
Enter fullscreen mode Exit fullscreen mode

Now, let's push the image made from it to Docker Hub:

$docker push steadylearner/ubuntu_node
Enter fullscreen mode Exit fullscreen mode

and use this:

$docker run -it steadylearner/ubuntu_node bash
Enter fullscreen mode Exit fullscreen mode

or this, to verify the result:

$docker history steadylearner/ubuntu_node
Enter fullscreen mode Exit fullscreen mode

8. Conclusion

I hope you made it all work. We learned how to install Docker and make it work with Nginx, how to make a Docker container and image and upload them into Docker Hub.

There are many things to learn. But everything will be easier with examples. In the later posts on the Steadylearner Blog, we will learn how to deploy the web app with Elastic Beanstalk from AWS and Dockerfile. We will also learn how to deploy micro services with ECS, Cloud​Formation, docker-compose.yml etc. So, keep an eye out for updates!

Stay on top of the latest Steadylearner content: follow me on Twitter.

If you need to hire a developer, you can contact me.

Thank you! Share this post with others and help us to grow and improve.

Reviewed by Mariana Santos

Refer to these commands if you want more. Use the ID or the name of the containers for them.

Logs of the container

$docker logs containerid | name
Enter fullscreen mode Exit fullscreen mode

History of the image

#docker history steadylearner/ubuntu_node
Enter fullscreen mode Exit fullscreen mode

Remove unused images

$docker images
$docker image rm dockerimagename or docker rmi
Enter fullscreen mode Exit fullscreen mode

Rename the container

$docker rename randomname whatyouwant
Enter fullscreen mode Exit fullscreen mode

Pause and unpause the containers

$docker pause containerid | name
$docker ps -a
$docker unpuase containerid | name
$docker ps -a
Enter fullscreen mode Exit fullscreen mode

Start and stop them

$docker stop containerid | name
$docker ps -a
$docker start containerid | name
$docker ps -a
Enter fullscreen mode Exit fullscreen mode

Remove containers

$docker container rm containerid | name
Enter fullscreen mode Exit fullscreen mode

Top comments (0)