# CPSC 354 in Docker ## Introduction The first section builds a docker image isolated from your filesystem. This has its advantages but for working on an assignment go to the second section. ## Running Haskell in a Docker Container (This is a good solution if you want to isolate your container from your filesystem but for the work on an assignment you probably want to go to the next section.) <font color=silver> The dockerfile, named `dockerfile`, ``` FROM haskell:8 COPY . . WORKDIR / RUN cabal update; RUN cabal install BNFC; RUN cabal install alex; RUN cabal install happy; CMD /bin/bash ``` should be in the directory in which you want to run Haskell. The first time, you need to build a docker image, called `haskell-pl` here, from the dockerfile: ``` docker build -t haskell-pl . ``` Now you can create a container ``` docker run -it --rm haskell-pl ``` and run Haskell inside. To exit the container use `control-d`. (You don't need to `build` again if you want to `run` again.) With this set-up, if you want to modify your files, you should exit the container, make your modifications, build run it again. Changes made inside the container will not persist after exit. This point is addressed in the next section. </font> ## Connecting Docker to Your Filesystem In this section, I want my own local file system and the filesystem of the container to overlap. The basic idea is to use `docker` with the `-v` option in order to mount a local directory into the container. In this situation, we do not want to `COPY . .` the local directory, so we start from the `dockerfile` ``` FROM haskell:8 WORKDIR / RUN cabal update; RUN cabal install BNFC; RUN cabal install alex; RUN cabal install happy; CMD /bin/bash ``` I create a docker image with `docker build -t haskell-pl2 .` as before. Then I run in the local directory in which I want to run Haskell: [^windows] [^windows]: Windows may need some care with getting the path right ... I am not a Windows user ... one of my students successfully tried `docker run -d -it --name lambdanat -v "C:\Users...\CPSC354\LambdaNat2023":/home haskell-pl2` ``` docker run -d -it --name lambdanat -v path_to_local_dir:/home haskell-pl2 docker start lambdanat docker attach lambdanat cd home ls ``` After `ls` I now see the content of the mounted directory. **Workflow:** I can now open, change and modify files in any editor outside of the container and then run them inside the container. ## Docker in Visual Studio Code As we have seen, with the set-up above it is possible to modify files outside of a container and then run them inside a container. But it is also possible to attach the editor VSCode to the container and modify the files directly in the container. In VSCode install the docker extension and the *Remote Development* extension, which yields the following in the lower left corner. ![](https://i.imgur.com/sG6Dxkz.png =100x) Click the green (or blue) icon and select "Attach to Running Container ...". Now you should be able to use vscode inside the container. ## Final Remarks Show running containers: `docker ps` Show all stopped containers: `docker ps -a | grep Exit` Show all containers: `docker ps -a` Stop a container: `docker stop lambdanat` Stop all containers: `docker stop $(docker ps -a -q)` Remove a stopped container: `docker rm lambdanat` Remove all stopped containers: `docker rm $(docker ps -a -q)` ## Acknowledgements Thanks to Rene German, Dan Haub, Chris Kruki and everybody who helped me out with docker in previous years. ## References - Docker Docs: [Use volumes](https://docs.docker.com/storage/volumes/) ... "Volumes are the preferred mechanism for persisting data generated by and used by Docker containers." - CPSC 354 in Docker [(2021)](https://hackmd.io/@alexhkurz/HyhqVjZQt), [(2022)](https://hackmd.io/@alexhkurz/rkwoB7tVa). ---