Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Amazon Linux 2 - install docker & docker-compose using 'sudo amazon-linux-extras' command

UPDATE (March 2020, thanks @ic): I don't know the exact AMI version but yum install docker now works on the latest Amazon Linux 2. The instructions below may still be relevant depending on the vintage AMI you are using.

Amazon changed the install in Linux 2. One no-longer using 'yum' See: https://aws.amazon.com/amazon-linux-2/release-notes/

Docker CE Install

sudo amazon-linux-extras install docker
sudo service docker start
sudo usermod -a -G docker ec2-user

Make docker auto-start

sudo chkconfig docker on

Because you always need it....

sudo yum install -y git

Reboot to verify it all loads fine on its own.

sudo reboot

docker-compose install

Copy the appropriate docker-compose binary from GitHub:

sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

NOTE: to get the latest version (thanks @spodnet): sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

Fix permissions after download:

sudo chmod +x /usr/local/bin/docker-compose

Verify success:

docker-compose version

@parnpresso
Copy link

parnpresso commented Apr 15, 2021

The official docker/compose repo does not currently have arm64 builds, but you can use linuxserver/docker-docker-compose instead.

Here is a trimmed down setup cmd for docker/docker-compose that works for Amazon Linux 2 arm64 as well as x86:

sudo yum -y install docker; \
 sudo service docker start; \
 sudo usermod -a -G docker ec2-user; \
 sudo chkconfig docker on; \
 sudo curl -L --fail https://raw.githubusercontent.com/linuxserver/docker-docker-compose/master/run.sh -o /usr/local/bin/docker-compose; \
 sudo chmod +x /usr/local/bin/docker-compose; \
 sudo reboot;

Here is a quick and dirty cmd that I threw together.

It additionally installs git/yum-cron, configures yum-cron to automatically install security updates hourly, and sets a system wide env to specify that I want the alpine tag of docker-compose.

sudo yum -y install git docker yum-cron; \
 sudo sed -i '/update_cmd/s/= .*/= security/' /etc/yum/yum-cron-hourly.conf; \
 sudo sed -i '/update_messages/s/= .*/= no/' /etc/yum/yum-cron-hourly.conf; \
 sudo sed -i '/apply_updates/s/= .*/= yes/' /etc/yum/yum-cron-hourly.conf; \
 sudo sed -i '/download_updates/s/= .*/= yes/' /etc/yum/yum-cron-hourly.conf; \
 sudo sed -i '/download_updates/s/= .*/= no/' /etc/yum/yum-cron.conf; \
 sudo service yum-cron start; \
 sudo chkconfig yum-cron on; \
 sudo service docker start; \
 sudo usermod -a -G docker ec2-user; \
 sudo chkconfig docker on; \
 sudo sh -c "echo 'export DOCKER_COMPOSE_IMAGE_TAG=ghcr.io/linuxserver/docker-compose:alpine' >> /etc/profile.d/dockerComposeAlpineImageTagEnv.sh"; \
 sudo curl -L --fail https://raw.githubusercontent.com/linuxserver/docker-docker-compose/master/run.sh -o /usr/local/bin/docker-compose; \
 sudo chmod +x /usr/local/bin/docker-compose; \
 sudo reboot;

It's work! Thank you 🚀

@Sandeepdemo
Copy link

Sandeepdemo commented Apr 24, 2021

I am a very beginners level comprehensive Explanation. 100/100 marks, I have followed the given instruction and got successfully.

@herdsothom
Copy link

herdsothom commented May 12, 2021

After the fix permissions step (for docker-compose), you need to add a step to move docker-compose to /usr/bin (from /usr/local/bin)

@shafeer-tmmt
Copy link

shafeer-tmmt commented Jun 15, 2021

Thanks much

@xuexb
Copy link

xuexb commented Jul 19, 2021

Thank you

@lassebenni
Copy link

lassebenni commented Jul 31, 2021

Thanks, working on an EC2 instance with Amazon Linux v2!

@VravikumarV
Copy link

VravikumarV commented Aug 1, 2021

Thanks much, i tried this to setup docker and docker-compose in EC2 but left with below error upon running docker-compose up -d

Any help in fixing below issue is much appreciated!

ec2-user@ip-172-32-43-155 ~]$ docker-compose up -d
Traceback (most recent call last):
File "urllib3/connectionpool.py", line 677, in urlopen
File "urllib3/connectionpool.py", line 392, in _make_request
File "http/client.py", line 1277, in request
File "http/client.py", line 1323, in _send_request
File "http/client.py", line 1272, in endheaders
File "http/client.py", line 1032, in _send_output
File "http/client.py", line 972, in send
File "docker/transport/unixconn.py", line 43, in connect
FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "requests/adapters.py", line 449, in send
File "urllib3/connectionpool.py", line 727, in urlopen
File "urllib3/util/retry.py", line 410, in increment
File "urllib3/packages/six.py", line 734, in reraise
File "urllib3/connectionpool.py", line 677, in urlopen
File "urllib3/connectionpool.py", line 392, in _make_request
File "http/client.py", line 1277, in request
File "http/client.py", line 1323, in _send_request
File "http/client.py", line 1272, in endheaders
File "http/client.py", line 1032, in _send_output
File "http/client.py", line 972, in send
File "docker/transport/unixconn.py", line 43, in connect
urllib3.exceptions.ProtocolError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "docker/api/client.py", line 214, in _retrieve_server_version
File "docker/api/daemon.py", line 181, in version
File "docker/utils/decorators.py", line 46, in inner
File "docker/api/client.py", line 237, in _get
File "requests/sessions.py", line 543, in get
File "requests/sessions.py", line 530, in request
File "requests/sessions.py", line 643, in send
File "requests/adapters.py", line 498, in send
requests.exceptions.ConnectionError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "docker-compose", line 3, in
File "compose/cli/main.py", line 81, in main
File "compose/cli/main.py", line 200, in perform_command
File "compose/cli/command.py", line 70, in project_from_options
File "compose/cli/command.py", line 153, in get_project
File "compose/cli/docker_client.py", line 43, in get_client
File "compose/cli/docker_client.py", line 170, in docker_client
File "docker/api/client.py", line 197, in init
File "docker/api/client.py", line 222, in _retrieve_server_version
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

@caohunan
Copy link

caohunan commented Aug 4, 2021

still works!

@drupol
Copy link

drupol commented Aug 4, 2021

Thanks !

@michaelact
Copy link

michaelact commented Aug 9, 2021

Thanks!!

@zanio
Copy link

zanio commented Aug 13, 2021

Still working. Thanks!

@SPONGE-JL
Copy link

SPONGE-JL commented Aug 22, 2021

Perfect guide! Thank you :)

@evantlee
Copy link

evantlee commented Aug 24, 2021

To verify the command docker-compose version, I had to create a symbolic link using the command ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose.

Confirming this. I had to do the same - and it's actually on the docker install instructions as well! Thanks for the helpful guide!

@axlfire1
Copy link

axlfire1 commented Aug 26, 2021

Excellent thanks

@mifa-mail
Copy link

mifa-mail commented Sep 3, 2021

thanks !

@spellingb
Copy link

spellingb commented Sep 16, 2021

superb. ty kind internet stranger

@FutureGadget
Copy link

FutureGadget commented Sep 26, 2021

This still works like a charm. TY!

@dzmitrykliapkou
Copy link

dzmitrykliapkou commented Sep 30, 2021

Be careful with this part now:

NOTE: to get the latest version (thanks @spodnet): 
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

Docker-compose v2.0.0 has been released and they decided to change x86_64 to amd64 in the filename for the release. So as a result this link with $(uname -m) does not work anymore

@DevJulianSalas
Copy link

DevJulianSalas commented Oct 7, 2021

Thanks!

@Spunkie
Copy link

Spunkie commented Oct 7, 2021

Docker-compose v2.0.0 has been released and they decided to change x86_64 to amd64 in the filename for the release. So as a result this link with $(uname -m) does not work anymore

Looks like this was resolved quickly with a v2.0.1 release.


Also with the release of docker-compose v2 the official repo now includes arm64 builds. 🎉

@rakeshhegishte
Copy link

rakeshhegishte commented Oct 9, 2021

Thanks a lot. it worked for me

@ppshein
Copy link

ppshein commented Oct 22, 2021

ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

that's sweet.

@Chinweoke18
Copy link

Chinweoke18 commented Nov 2, 2021

@ozbillwang thanks a lot, solved it for me

@hscale
Copy link

hscale commented Nov 8, 2021

Should check PATH for run Docker-composer:

[root@ip-172-30-xx-xx bin]# PATH=/usr/local/bin:$PATH
[root@ip-172-30-xx-xx bin]# echo $PATH
/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

@tranvanthuc
Copy link

tranvanthuc commented Jan 22, 2022

Thank you so much. It's worked!

@timogoosen
Copy link

timogoosen commented Jan 28, 2022

The docker-compose part you can skip and just do:

sudo pip3 install docker-compose

You can do this right after installing docker, then do a reboot, then docker-compose should work fine.
Thanks for sharing this gist.
Was very helpful!

@eit
Copy link

eit commented Feb 17, 2022

use pip3 also works. thanks

@4RSIM3R
Copy link

4RSIM3R commented Feb 21, 2022

sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/bin/docker-compose && chmod +x /usr/bin/docker-compose && docker-compose --version

@Ucinorn
Copy link

Ucinorn commented Mar 1, 2022

For those who get all the way down here: the following userdata script works great for me on a basic Amazon Linux ARM EC2 instance:

#!/bin/bash
yum update
yum -y install docker
service docker start
usermod -a -G docker ec2-user
chkconfig docker on
pip3 install docker-compose
reboot

Note userdata always runs as root so no need to use sudo

After its finished initialising run this to see the output of the userscript to ensure it all ran as expected and for debugging:

sudo cat /var/log/cloud-init-output.log

@victorsferreira
Copy link

victorsferreira commented May 21, 2022

Guys in case you followed Docker instructions first you need to delete the repo before continuing:
sudo rm /etc/yum.repos.d/docker-ce.repo

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