Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker with amazonlinux

Complete newbie with Docker, but the concepts are sticking.

Goal

Create a container locally that matches my prod EC2 instance. Upgrade PHP7 and test my application.

Approach

My first step was to create just a basic box amazon linux box. Mount it and run the commands from the CLI. Then I can slowly build up the commands to put in my Dockerfile.

Problem

I build the box fine run yum update -Y and see the updates. However, when I run docker ps the image isn't listed? So I can't mount it. My composerfile is as follows...

FROM amazonlinux:2017.03

RUN yum update -y

Why is the box not remaining in an open state?

like image 605
sufcboy Avatar asked Jul 28 '17 12:07

sufcboy


People also ask

What is an Amazon Linux 2 Docker container image?

Amazon Linux Docker container images contain a subset of the packages in the images for use on EC2 and as VMs in on-premises scenarios. The container images can be configured to use any of the full set of packages in images for EC2 and on-premises use. The Amazon Linux 2 container images comes with Extras included. What is an Amazon Linux 2 Extra?

Is it possible to run Amazon Linux in Docker?

Amazonlinux in Docker (created via FROM amazonlinux:2) is so bare and empty that it doesn't even have basic stuffs like sudo or passwd .) New AWS EC2 instances do. In order to have your serviced properly working (to start any daemon, including Docker Daemon), you need to have /usr/sbin/init be there (via yum install initscripts and actually called.

How to install Docker on Amazon EC2?

You can install Docker on an Amazon EC2 instance by launching an instance of Amazon Linux 2 or Amazon Linux AMI on top. Check out Launching an Instance in the Amazon EC2 User Guide for Linux Instances for more details. Which Editions Of Docker Can We Install In Aws? The AWS free tier allows the installation of Docker for AWS.

How do I run Docker on a Linux machine?

Launch Docker Machine. Connect to Docker Linux EC2 Terminal through Putty. Switch to root user. Update Server Packages. Install Docker Package. Start the service of Docker. Verify the status of Docker. See the list of all images inside your machine. See the list of all running containers. See the list of all containers inside your machine. 1.


1 Answers

I had the same goal of testing instances in development environment and initially I thought it should be as easy as docker run amazonlinux:2 -it. But I was so wrong and it took me almost one full day to get it to work!

Funny thing is when you google "amazonlinux Docker" it's often people trying to install "Docker in amazonlinux", but here we want to install "amazonlinux in Docker"!

We also want to install Docker in that amazonlinux, so basically "Docker in amazonlinux in Docker" which is "Docker in Docker" eventually! ;D*

My findings:

  • Amazonlinux in Docker (created via FROM amazonlinux:2) is so bare and empty that it doesn't even have basic stuffs like sudo or passwd.) New AWS EC2 instances do.
  • In order to have your serviced properly working (to start any daemon, including Docker Daemon), you need to have /usr/sbin/init be there (via yum install initscripts and actually called. However, the meat you want to play with need your shell to start from /bin/bash.
  • You are running a Docker within a Docker. That needs to be priviledged from the host in your docker run via --priviledged.
  • You need to share the /sys/fs/cgroup from your host machine (it can be read-only) for it to be able to properly initialize docker daemon.

My solution:

1) To fulfill the first two issues above, your Dockerfile can be:

FROM amazonlinux:2

RUN yum update -y && yum install -y initscripts;

CMD ["/usr/sbin/init"]

2) Build an image from it, e.g. docker build . -t ax1

3) Then, to address the latter two issues above, run a detached (running in background) container from it, priviledged, with a shared volume to your /sys/fs/cgroup. e.g.

docker run --name ac11 -d --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro ax1

4) Finally you can bash into it using docker exec -it ac11 bash

5) Now, it's very close to a new EC2 instance. (Yet, missing sudo, actual ec2-user and other stuffs that we skipped in our Dockerfile to keep this solution simple.)

Anyway, now you can install docker as instructed by AWS Docs. That is, once you are in the container, do:

amazon-linux-extras install -y docker;

and then restart the docker service once:

service docker restart;

Now, docker ps should be working!

like image 126
Aidin Avatar answered Oct 01 '22 09:10

Aidin