Docker on OSX

Page content

Some Notes Based on this Video: https://www.youtube.com/watch?v=bhBSlnQcq2k

Download Docker

https://docs.docker.com/get-docker/

Download Nginx Image

https://hub.docker.com/_/nginx

docker pull nginx

Run Image

docker run nginx
docker run nginx:latest
docker run -d nginx:latest
docker run -d -p 80 nginx:latest
docker run -d -p 8080:80 nginx:latest
docker run -d -p 3000:80 -p 8080:80 nginx:latest

Access Webserver

mbp:~ stoege$ docker run -d -p 8080:80 nginx:latest
5c7a945caa59f14e35932f3d4470c9b9afc0307dac34e01947d41adbcdfda091

mbp:~ stoege$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                  NAMES
5c7a945caa59        nginx:latest        "/docker-entrypoint.…"   About a minute ago   Up About a minute   0.0.0.0:8080->80/tcp   laughing_cartwright

open Brower http://localhost:8080 -> Welcome Page

Stop Container

mbp:~ stoege$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
5c7a945caa59        nginx:latest        "/docker-entrypoint.…"   4 minutes ago       Up 4 minutes        0.0.0.0:8080->80/tcp   laughing_cartwright

mbp:~ stoege$ docker stop 5c7a945caa59
5c7a945caa59

mbp:~ stoege$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Managing Containers

mbp:~ stoege$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                                        NAMES
fd009cac31b1        nginx:latest        "/docker-entrypoint.…"   About a minute ago   Up About a minute   0.0.0.0:3000->80/tcp, 0.0.0.0:8080->80/tcp   heuristic_easley

docker stop fd009cac31b1
docker stop heuristic_easley
docker start heuristic_easley

List Containers

mbp:~ stoege$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
fd009cac31b1        nginx:latest        "/docker-entrypoint.…"   3 minutes ago       Exited (0) 23 seconds ago                       heuristic_easley
ba61f5233516        nginx:latest        "/docker-entrypoint.…"   6 minutes ago       Exited (0) 3 minutes ago                        gallant_margulis
5c7a945caa59        nginx:latest        "/docker-entrypoint.…"   17 minutes ago      Exited (0) 12 minutes ago                       laughing_cartwright
4a03c1689f67        nginx:latest        "/docker-entrypoint.…"   21 minutes ago      Exited (0) 18 minutes ago                       strange_brown
07ce79209925        nginx:latest        "/docker-entrypoint.…"   21 minutes ago      Exited (0) 21 minutes ago                       tender_sinoussi
82b7f68e9a77        nginx:latest        "/docker-entrypoint.…"   23 minutes ago      Exited (0) 22 minutes ago                       mystifying_shirley
a184f59e1c8f        nginx               "/docker-entrypoint.…"   24 minutes ago      Exited (0) 23 minutes ago                       funny_pasteur

Delete Containers

mbp:~ stoege$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
fd009cac31b1        nginx:latest        "/docker-entrypoint.…"   3 minutes ago       Exited (0) 23 seconds ago                       heuristic_easley
ba61f5233516        nginx:latest        "/docker-entrypoint.…"   6 minutes ago       Exited (0) 3 minutes ago                        gallant_margulis
5c7a945caa59        nginx:latest        "/docker-entrypoint.…"   17 minutes ago      Exited (0) 12 minutes ago                       laughing_cartwright
4a03c1689f67        nginx:latest        "/docker-entrypoint.…"   21 minutes ago      Exited (0) 18 minutes ago                       strange_brown
07ce79209925        nginx:latest        "/docker-entrypoint.…"   21 minutes ago      Exited (0) 21 minutes ago                       tender_sinoussi
82b7f68e9a77        nginx:latest        "/docker-entrypoint.…"   23 minutes ago      Exited (0) 22 minutes ago                       mystifying_shirley
a184f59e1c8f        nginx               "/docker-entrypoint.…"   24 minutes ago      Exited (0) 23 minutes ago                       funny_pasteur

mbp:~ stoege$ docker rm 82b7f68e9a77
82b7f68e9a77

mbp:~ stoege$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
fd009cac31b1        nginx:latest        "/docker-entrypoint.…"   5 minutes ago       Exited (0) 2 minutes ago                        heuristic_easley
ba61f5233516        nginx:latest        "/docker-entrypoint.…"   8 minutes ago       Exited (0) 5 minutes ago                        gallant_margulis
5c7a945caa59        nginx:latest        "/docker-entrypoint.…"   19 minutes ago      Exited (0) 14 minutes ago                       laughing_cartwright
4a03c1689f67        nginx:latest        "/docker-entrypoint.…"   23 minutes ago      Exited (0) 19 minutes ago                       strange_brown
07ce79209925        nginx:latest        "/docker-entrypoint.…"   23 minutes ago      Exited (0) 23 minutes ago                       tender_sinoussi
a184f59e1c8f        nginx               "/docker-entrypoint.…"   26 minutes ago      Exited (0) 24 minutes ago                       funny_pasteur

Delete all Containers

mbp:~ stoege$ docker ps -a -q
fd009cac31b1
ba61f5233516
5c7a945caa59
4a03c1689f67
07ce79209925
a184f59e1c8f

mbp:~ stoege$ docker rm $(docker ps -aq)
fd009cac31b1
ba61f5233516
5c7a945caa59
4a03c1689f67
07ce79209925
a184f59e1c8f

mbp:~ stoege$ docker ps -a -q
mbp:~ stoege$

Name Containers

mbp:~ stoege$ docker run --name website -d -p 8080:80 nginx:latest
f75d0ad383b17965227253dcda417dc38334db50fd7737e29bd4e40b8b6e80a2

mbp:~ stoege$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
f75d0ad383b1        nginx:latest        "/docker-entrypoint.…"   3 seconds ago       Up 2 seconds        0.0.0.0:8080->80/tcp   website

docker stop website
docker start website

Build own Content

mkdir html
echo "hello 1234" > html/index.html

echo "FROM nginx"                        > Dockerfile
echo "COPY html /usr/share/nginx/html"  >> Dockerfile
docker build -t mynginx .

docker run --name mywebsite -d -p 8080:80 mynginx 

docker images
docker ps

Mounted Volume RO

mkdir html
echo "hello mounted volume" > html/index.html
cd html
docker run --name website -v $(pwd):/usr/share/nginx/html:ro -d -p 8080:80 nginx

http://localhost:8080

Running Bash in Docker

docker exec -it website bash

root@4dc1d54b4ffe:# touch /usr/share/nginx/html/test.html
touch: cannot touch '/usr/share/nginx/html/test.html': Read-only file system

Mounted Volume RW

docker run --name website -v $(pwd):/usr/share/nginx/html -d -p 8080:80 nginx
docker exec -it website bash
root@4dc1d54b4ffe:# touch /usr/share/nginx/html/test.html

Download Bootstrap One Page Template

https://bootstrapmade.com/bootstrap-one-page-templates/ Download Page, Extract, Adapt, Enjoy

Share Volumes on Docker Images

mbp:test003 stoege$ docker run --name website-copy --volumes-from website -d -p 8081:80 nginx
de2415e10c94bf62acdab7910b8dc805e388ea8997c6f0436a595162f23c0138

mbp:test003 stoege$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
de2415e10c94        nginx               "/docker-entrypoint.…"   3 seconds ago       Up 2 seconds        0.0.0.0:8081->80/tcp   website-copy
e0e1b981469c        nginx               "/docker-entrypoint.…"   4 minutes ago       Up 4 minutes        0.0.0.0:8080->80/tcp   website

http://localhost:80801

Build Docker File Image

cat << 'EOF' > Dockerfile
FROM nginx:latest
ADD . /usr/share/nginx/html
EOF

docker build --tag website:latest .
docker run --name website2 -d -p 8080:80 website

Template

sha256: 209fbc6038ce03cd5488c6b28b350d8a7c55450f81fd590df18d1e2ca8a563c9