Skip to content

Instantly share code, notes, and snippets.

@elstr
Last active February 1, 2021 12:36
Show Gist options
  • Save elstr/7f04012388d2582e838dbda08efeefde to your computer and use it in GitHub Desktop.
Save elstr/7f04012388d2582e838dbda08efeefde to your computer and use it in GitHub Desktop.

Revisions

  1. elstr revised this gist Feb 1, 2021. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions DOCKER-INTRO.md
    Original file line number Diff line number Diff line change
    @@ -98,9 +98,9 @@ Docker command:
    `docker run -p 3306:3306 -d --name db-container-test -e MYSQL_USER=user -e MYSQL_PASSWORD=mysql -e MYSQL_ROOT_PASSWORD=root mysql:5.7`

    ### In detail
    `-p` map any request to port 3306 to my docker container port 3306 (3306 is the default port for MySQL).
    `-d` detached mode: this means the container runs in the background.
    `--name` allows us to assign a name to the container so we can interact with it if needed and also identify it.
    `-p` map any request to port 3306 to my docker container port 3306 (3306 is the default port for MySQL).
    `-d` detached mode: this means the container runs in the background.
    `--name` allows us to assign a name to the container so we can interact with it if needed and also identify it.

    Resulting
    ```
  2. elstr revised this gist Feb 1, 2021. 1 changed file with 18 additions and 18 deletions.
    36 changes: 18 additions & 18 deletions DOCKER-INTRO.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ We will use the hello.txt file for the example
    `docker run -v $(pwd):/app alpine cmd-to-exec`

    ### In detail
    `-v` : mountes a volume in the container
    `-v` mountes a volume in the container
    `$(pwd)` gets the current directory
    `:/app` mountes the $(pwd) in the /app directory of my container
    `alpine` the type of img we want the container to run
    @@ -34,10 +34,10 @@ Hello from container
    `docker run -it -v $(pwd):/app alpine /bin/sh`

    ### In detail
    `-i` run interactive
    `-t` run with terminal access
    `-it` run interactive with terminal access
    `/bin/sh` opens up a shell session
    `-i` run interactive
    `-t` run with terminal access.
    `-it` run interactive with terminal access.
    `/bin/sh` opens up a shell session.

    Resulting:

    @@ -57,9 +57,9 @@ Hello from container
    `docker run node:latest node -e "console.log(process.version)"`

    ### In detail
    `node:latest` specifies the version of the node img we want to use
    `node -e` executes node and tells it to evaluate the next command
    `"console.log(process.version)"` the command that node evaluates
    `node:latest` specifies the version of the node img we want to use
    `node -e` executes node and tells it to evaluate the next command.
    `"console.log(process.version)"` the command that node evaluates.

    Resulting

    @@ -88,18 +88,19 @@ Hola Lele

    ## Running a DB in a container
    In this case we will run a MySQL db. MySQL needs a few env variables: MYSQL_USER, MYSQL_PASSWORD, MYSQL_ROOT_PASSWORD.
    We pass those values to our container using the `-e` flag. Each `-e` represents ONE env variable, so we pass it 3 times one for each env var.
    We pass those values to our container using the `-e` flag.
    Each `-e` represents ONE env variable, so we pass it 3 times one for each env var.
    `-e MYSQL_USER=user -e MYSQL_PASSWORD=mysql -e MYSQL_ROOT_PASSWORD=root`

    The docker img we are going to use is mysql version 5.7 => `mysql:5.7`

    Docker command:
    Docker command:
    `docker run -p 3306:3306 -d --name db-container-test -e MYSQL_USER=user -e MYSQL_PASSWORD=mysql -e MYSQL_ROOT_PASSWORD=root mysql:5.7`

    ### In detail
    `-p` map any request to port 3306 to my docker container port 3306 (3306 is the default port for MySQL)
    `-d` detached mode: this means the container runs in the background
    `--name` allows us to assign a name to the container so we can interact with it if needed and also identify it
    `-p` map any request to port 3306 to my docker container port 3306 (3306 is the default port for MySQL).
    `-d` detached mode: this means the container runs in the background.
    `--name` allows us to assign a name to the container so we can interact with it if needed and also identify it.

    Resulting
    ```
    @@ -124,8 +125,7 @@ a4ad797fef2714428a22817b5ef60e40a87cf1f641560c903b8a4209e843026b

    ## List the running containers

    To list the running containers we use the command
    `docker ps`
    To list the running containers we use the command `docker ps`

    Resulting:

    @@ -144,7 +144,7 @@ Resulting:
    a4ad797fef27
    ```

    If you relaunch the same container you'll get the following error:
    If you relaunch the same container you'll get the following error:
    `docker run -p 3306:3306 -d --name db-container-test -e MYSQL_USER=user -e MYSQL_PASSWORD=mysql -e MYSQL_ROOT_PASSWORD=root mysql:5.7`

    Resulting:
    @@ -153,8 +153,8 @@ docker: Error response from daemon: Conflict. The container name "/db-container-
    See 'docker run --help'.
    ```

    wtf?
    since the container already has a name assigned it conflicts with the container we want to launch
    wtf?
    since the container already has a name assigned it conflicts with the container we want to launch

    How to fix this?
    1) remove the contianer using `docker rm container-name`
  3. elstr created this gist Feb 1, 2021.
    174 changes: 174 additions & 0 deletions DOCKER-INTRO.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,174 @@
    # Docker 101

    We will use the hello.txt file for the example
    `echo "Hello from container" > hello.txt`

    ## Map directory to container (aka copy local dir to container dir)

    `docker run -v $(pwd):/app alpine cmd-to-exec`

    ### In detail
    `-v` : mountes a volume in the container
    `$(pwd)` gets the current directory
    `:/app` mountes the $(pwd) in the /app directory of my container
    `alpine` the type of img we want the container to run
    `cmd-to-exec` example: `cat /app/hello.txt`

    ### Example
    `docker run -v $(pwd):/app alpine cat /app/hello.txt`

    Resulting:

    ```
    ➜ docker run -v $(pwd):/app alpine cat /app/hello.txt
    Unable to find image 'alpine:latest' locally
    latest: Pulling from library/alpine
    4c0d98bf9879: Already exists
    Digest: sha256:08d6ca16c60fe7490c03d10dc339d9fd8ea67c6466dea8d558526b1330a85930
    Status: Downloaded newer image for alpine:latest
    Hello from container
    ```

    ## Connect to the container and do stuff inside it

    `docker run -it -v $(pwd):/app alpine /bin/sh`

    ### In detail
    `-i` run interactive
    `-t` run with terminal access
    `-it` run interactive with terminal access
    `/bin/sh` opens up a shell session

    Resulting:

    ```
    ➜ docker run -it -v $(pwd):/app alpine /bin/sh
    # cd app
    # ls
    apiKeys.js config.js index.js package-lock.json tmp
    assets hello.txt node_modules package.json
    # cat hello.txt
    Hello from container
    # exit
    ```

    ## Executing node commands

    `docker run node:latest node -e "console.log(process.version)"`

    ### In detail
    `node:latest` specifies the version of the node img we want to use
    `node -e` executes node and tells it to evaluate the next command
    `"console.log(process.version)"` the command that node evaluates

    Resulting

    ```
    ➜ docker run node:latest node -e "console.log(process.version)"
    v15.7.0
    ```

    #### Note:
    I can have `locally` (aka in my machine, not in a container) a different version installed:
    ```
    ➜ node -v
    v12.15.0
    ```

    ## Pass ENV vars to a container:

    `docker run -e NAME=Lele node:latest node -e "console.log('Hola ' + process.env.NAME)"`

    Resulting

    ```
    ➜ docker run -e NAME=Lele node:latest node -e "console.log('Hola ' + process.env.NAME)"
    Hola Lele
    ```

    ## Running a DB in a container
    In this case we will run a MySQL db. MySQL needs a few env variables: MYSQL_USER, MYSQL_PASSWORD, MYSQL_ROOT_PASSWORD.
    We pass those values to our container using the `-e` flag. Each `-e` represents ONE env variable, so we pass it 3 times one for each env var.
    `-e MYSQL_USER=user -e MYSQL_PASSWORD=mysql -e MYSQL_ROOT_PASSWORD=root`

    The docker img we are going to use is mysql version 5.7 => `mysql:5.7`

    Docker command:
    `docker run -p 3306:3306 -d --name db-container-test -e MYSQL_USER=user -e MYSQL_PASSWORD=mysql -e MYSQL_ROOT_PASSWORD=root mysql:5.7`

    ### In detail
    `-p` map any request to port 3306 to my docker container port 3306 (3306 is the default port for MySQL)
    `-d` detached mode: this means the container runs in the background
    `--name` allows us to assign a name to the container so we can interact with it if needed and also identify it

    Resulting
    ```
    ➜ docker run -p 3306:3306 -d --name db-container-test -e MYSQL_USER=user -e MYSQL_PASSWORD=mysql -e MYSQL_ROOT_PASSWORD=root mysql:5.7
    Unable to find image 'mysql:5.7' locally
    5.7: Pulling from library/mysql
    a076a628af6f: Already exists
    f6c208f3f991: Pull complete
    88a9455a9165: Pull complete
    406c9b8427c6: Pull complete
    7c88599c0b25: Pull complete
    25b5c6debdaf: Pull complete
    43a5816f1617: Pull complete
    1831ac1245f4: Pull complete
    37677b8c1f79: Pull complete
    27e4ac3b0f6e: Pull complete
    7227baa8c445: Pull complete
    Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
    Status: Downloaded newer image for mysql:5.7
    a4ad797fef2714428a22817b5ef60e40a87cf1f641560c903b8a4209e843026b
    ```

    ## List the running containers

    To list the running containers we use the command
    `docker ps`

    Resulting:

    ```
    ➜ docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    a4ad797fef27 mysql:5.7 "docker-entrypoint.s…" 30 seconds ago Up 29 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp db-container-test
    ```

    ## Stop a running container
    `docker stop container-id`

    Resulting:
    ```
    ➜ docker stop a4ad797fef27
    a4ad797fef27
    ```

    If you relaunch the same container you'll get the following error:
    `docker run -p 3306:3306 -d --name db-container-test -e MYSQL_USER=user -e MYSQL_PASSWORD=mysql -e MYSQL_ROOT_PASSWORD=root mysql:5.7`

    Resulting:
    ```
    docker: Error response from daemon: Conflict. The container name "/db-container-test" is already in use by container "a4ad797fef2714428a22817b5ef60e40a87cf1f641560c903b8a4209e843026b". You have to remove (or rename) that container to be able to reuse that name.
    See 'docker run --help'.
    ```

    wtf?
    since the container already has a name assigned it conflicts with the container we want to launch

    How to fix this?
    1) remove the contianer using `docker rm container-name`
    resulting:
    ```
    ➜ docker rm db-container-test
    db-container-test
    ```
    2) use the `--rm` flag when launching a container
    `docker run -p 3306:3306 -d --name db-container-test -e MYSQL_USER=user -e MYSQL_PASSWORD=mysql -e MYSQL_ROOT_PASSWORD=root mysql:5.7 --rm`
    resulting:
    ```
    ➜ docker run -p 3306:3306 -d --name db-container-test -e MYSQL_USER=user -e MYSQL_PASSWORD=mysql -e MYSQL_ROOT_PASSWORD=root mysql:5.7 --rm
    7e9c5d495d3d04f6b2d2ac314eb60b7b53e362ba331cdb113be4014ef140170a
    ```