Skip to content

Instantly share code, notes, and snippets.

@dmccuk
Last active November 17, 2022 10:00
Show Gist options
  • Save dmccuk/6d1ec92a86a0f29a57bdf76114cb318b to your computer and use it in GitHub Desktop.
Save dmccuk/6d1ec92a86a0f29a57bdf76114cb318b to your computer and use it in GitHub Desktop.

What are Linux Containers:

Linux containers, in short, contain applications in a way that keep them isolated from the host system that they run on. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. And they are designed to make it easier to provide a consistent experience as developers and system administrators move code from development environments into production in a fast and replicable way.

Use this link to install Docker on Ubuntu 20:

I'll walk through the install in the video. Here's the link: https://docs.docker.com/engine/install/ubuntu/

Check docker is running:

$ systemctl status docker

Build a container image

Get started by creating a static (basic) website on a container, running it locally on your vagrant VM and viewing it on a web browser.

For this demo I’m using a Ubuntu 20 AWS VM: ami-0194c3e07668a7e36

On your server, go to the directory holding the website file:

Create the structure and index.html file

$ mkdir src
$ cd src
/src$ vi index.html 

<!doctype html>
<title>LondonIAC meetup - Site Maintenance</title>
<style>
  body { text-align: center; padding: 150px; }
  h1 { font-size: 50px; }
  body { font: 20px Helvetica, sans-serif; color: #333; }
  article { display: block; text-align: left; width: 650px; margin: 0 auto; }
  a { color: #dc8100; text-decoration: none; }
  a:hover { color: #333; text-decoration: none; }
</style>

<article>
    <h1>We&rsquo;ll be back soon!</h1>
    <div>
        <p>Sorry for the inconvenience but we&rsquo;re performing some maintenance at the moment. If you need to you can always <a href="mailto:#">contact us</a>, otherwise we&rsquo;ll be back online shortly!</p>
    <img src="https://marcelorjava.files.wordpress.com/2014/04/dilbert.gif" alt="Dilbert">
        <p>&mdash; The Team</p>
    </div>
    <p>
</p>
</article>

Create the Dockerfile:

/src$ vi Dockerfile

FROM nginx:alpine
COPY . /usr/share/nginx/html
EXPOSE 80

Build the app:

/src$ sudo docker build -t meetup-app .
Sending build context to Docker daemon  3.584kB
Step 1/3 : FROM nginx:alpine
 ---> ea1193fd3dde
Step 2/3 : COPY . /usr/share/nginx/html
 ---> 33bab77e254d
Step 3/3 : EXPOSE 80
 ---> Running in 3c9cc1e254b2
Removing intermediate container 3c9cc1e254b2
 ---> 1e743fba89b1
Successfully built 1e743fba89b1
Successfully tagged meetup-app:latest

Check the image has been created:

/src$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
meetup-app           latest              1e743fba89b1        44 seconds ago      20.6MB

Run the container and specify the ports:

/src$ sudo docker run -d -p 80:80 meetup-app
91f42dc899dff09b4702227f5fc05409d0c79bd8905cf21b082e9702e2b806f7

/src$ sudo docker ps
CONTAINER ID  IMAGE      COMMAND       CREATED  STATUS    PORTS               NAMES
91f42dc899df  meetup-app  "nginx -g …"  10 secs  Up 8 secs 0.0.0.0:80->80/tcp  determined_germain

Check it worked:

/src$ curl localhost:80

You should see the index.html page printed out to the screen. We can also visit the webpage using the VM IP address from your web browser:

• You’ve created your first container to host a static website!

Pushing containers to docker hub:

Now you’ve created a new container and tested it, let’s push it up to docker hub so we can pull it down and use it later on.

Steps to complete:

Create a Docker-hub account or login to your existing account here: https://hub.docker.com

On your VM, login to docker hub via the command line:

/src$ sudo docker login --username=<your-userame>
Login Succeeded

Check the docker image you want to upload:

$ sudo docker images
REPOSITORY      TAG         IMAGE ID         CREATED             SIZE
meetup-app       latest      1e743fba89b1     15 minutes ago      20.6MB

Tag your image:

/src$ sudo docker tag 1e743fba89b1 dmccuk/meetup-app:first

Docker push username/repository.

/src$ sudo docker push dmccuk/meetup-app:first
The push refers to repository [docker.io/dmccuk/meetup-app]
9acad8977816: Pushed
fbe0fc9bcf95: Mounted from dmccuk/meetup-app
f1b5933fe4b5: Mounted from dmccuk/meetup-app
first: digest: sha256:2393c5afcec5a2d284c37b26f621d88be649fb98e049920dc70237adfe7143ca size: 946

Once the upload has finished, check your docker hub repository and confirm the container has been uploaded.

Docker Summary:

o Created your first container. o We’ve started it and can see it running. o Uploaded your container to Docker-Hub. o It’s now available to the world.

Good Job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment