A gentle introduction to Docker

in #technology6 years ago (edited)

Docker is an application that allows you to make and use containers, which are kind of light weight Virtual Machines. This is very useful since by using containers, you can easily save a whole linux environment and port it to another system in a fast and efficient manner.

In this tutorial, I will teach you how to make a simple Docker container that runs an SSH server. Imagine a situation where you want to give access to a server to multiple users, with a separate container allocated to each user on that server.


image address

For creating a Virtual Private Server (VPS), we can use DigitalOcean, which provides $100 worth of free resources for trial.

Installing Docker

First of all you'll need to install Docker on your VPS. To do that, follow the instructions given here.

Create the Docker image

Create a directory, called let's say "build". Go into that directory and save a file named Dockerfile with the following content. Besides this file, we will also create a script (start.sh), which would run whenever a docker container instantiates.


#Download base image
FROM ubuntu
RUN apt-get update
RUN apt-get install -y net-tools
RUN apt install -y openssh-client
RUN apt install -y openssh-server
COPY start.sh /usr/local/bin
RUN chmod u+x /usr/local/bin/start.sh
ENTRYPOINT start.sh

Here, first using the FROM command, we derive our new image from the vanilla Ubuntu image. Afterwards, we install the necessary tools required for the Open SSH server, using the RUN command. Then we copy the start.sh file (discussed later), to /usr/local/bin of the container. Afterwards, we give it the required permissions. ENTRYPOINT here specifies the command which is going to be run when a container instantiates. In this case, we would run the start.sh script.

Now, create the file start.sh and add the following content to it.


#!/bin/bash
useradd -m $USERNAME
echo "$USERNAME:abc" | chpasswd
service ssh start
tail -f /dev/null

The $USERNAME environment variable would be passed to the container when the docker image would be run (explained later on). This script will add the user $USERNAME to the container, set its password to "abc" and also create a home directory for that user. Afterwards, the SSH server would be started. The last line "tail -f /dev/null" here is meant to keep a foreground task active for an infinite amount of time. If this was not added, the docker container would exit immediately.

Build the docker image

Let's say your name is Jonathan. You can create the docker image by issuing the following command.


docker build . -t jonathan/ssh1

Now if you type docker images, you must be able to see the image jonathan/ssh1 under the REPOSITORY column.

Run the docker image

You can run the docker image you created by for example, typing the following


docker run -p 52019:22 -d -e USERNAME='david' jonathan/ssh1 

Bascially, here we have set the environment variable USERNAME to 'david', for the user 'david'. Since SSH server uses port 22, we have mapped this port to the port 52019 of the server running this container. If you now type docker ps -a, you'll see something like this.


CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS                   
05413540e30a        jonathan/ssh1       "/bin/sh -c start.sh"   3 seconds ago       Up 2 seconds        0.0.0.0:52019->22/tcp   

Now, david can logon to the container running on the server, by typing the following on his computer,


ssh -p 52019 david@<ip_address_of_the_server>

Pushing the container onto the docker hub repository

For this, first of all make an account on https://hub.docker.com/. On the server, then type docker login and enter your username and password. Assuming, your username on dockerhub is also jonathan, type the following on the server.


docker tag jonathan/ssh1 jonathan/ssh1
docker push jonathan/ssh1

After this, your docker image would be available on the docker hub repository, and you would be able to run it from anywhere by pulling it from the repository, using the docker pull command, which in this case, would be docker pull jonathan/ssh1.


I hope you found this tutorial useful. If you still have any questions, don't hesitate to ask me.

Sort:  

Good content... are you planning to post continuous tutorials like this..

Thanks. Yes, I'll do so, whenever I have some time :)

Hope i will make use of it.. i m a it engineer.. nd currently working on docker.. hope this will be a great start..

I wish I have one VPS server.
I will someday surely try the docker.
It's good to hear about new technology from you guys.
Thanks for sharing such new technology with us.
I hope to hear more of this stuff in the future.

If you are a student then you can try this: https://education.github.com/pack gives you access to different things , but one item in the pack that might interest you is:"$50 in platform credit for new users" for digitalocean. What computer are you on? If you are on a Mac or Windows then you can still try docker.

The problem is I am bachelor graduate student and have not join my masters.

Ok then I suggest try amazon AWS free tier. Just make sure you stay within the free tier.

Here are some beginner docker links for anyone interested:
https://severalnines.com/blog/mysql-docker-containers-understanding-basics
https://gist.github.com/ruanbekker/4e8e4ca9b82b103973eaaea4ac81aa5f
https://github.com/wsargent/docker-cheat-sheet
http://mherman.org/docker-workshop/#39

Please see my subreddit that I started for Linux related stuff especially for beginners reddit.com/r/linux_mentor. Feel free to post this article on there.

As a follower of @followforupvotes this post has been randomly selected and upvoted! Enjoy your upvote and have a great day!

Excellent post.

Nice to hear about docker.
I hope to get more contents like this in future.

Congratulations, your post received 9.57% up vote form @spydo courtesy of @hmushtaq! I hope, my gratitude will help you getting more visibility.
You can also earn by making delegation. Click here to delegate to @spydo and earn 95% daily reward payout! Follow this link to know more about delegation benefits.

You got a 1.37% upvote from @postpromoter courtesy of @hmushtaq!

Want to promote your posts too? Check out the Steem Bot Tracker website for more info. If you would like to support the development of @postpromoter and the bot tracker please vote for @yabapmatt for witness!

Loading...

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63098.06
ETH 2563.30
USDT 1.00
SBD 2.83