87 lines
2.8 KiB
Markdown
87 lines
2.8 KiB
Markdown
To enable shell history for Code-Server when running inside a Docker container, you need to configure the shell environment within the container properly. Below are the steps to achieve this, presented in Markdown format:
|
||
|
||
```markdown
|
||
# Enabling Shell History for Code-Server in a Docker Container
|
||
|
||
When working with Code-Server inside a Docker container, enabling shell history can enhance your workflow by maintaining a record of previously executed commands. Here’s a step-by-step guide to enable this feature:
|
||
|
||
## Step 1: Create a Dockerfile
|
||
|
||
You need to set up a Dockerfile that includes the necessary configurations.
|
||
|
||
```dockerfile
|
||
FROM codercom/code-server:latest
|
||
|
||
# Install necessary packages
|
||
USER root
|
||
RUN apt-get update && apt-get install -y \
|
||
bash \
|
||
&& rm -rf /var/lib/apt/lists/*
|
||
|
||
# Set up history file location
|
||
ENV HISTFILE="/config/.bash_history"
|
||
ENV HISTFILESIZE=1000
|
||
ENV HISTSIZE=1000
|
||
ENV HISTCONTROL=ignoredups:erasedups # Adjust to your preference
|
||
|
||
# Make sure the history file is created and accessible
|
||
RUN touch $HISTFILE && chown coder:coder $HISTFILE
|
||
|
||
USER coder
|
||
SHELL ["/bin/bash", "-c"]
|
||
```
|
||
|
||
## Step 2: Build the Docker Image
|
||
|
||
Build the Docker image from your Dockerfile.
|
||
|
||
```bash
|
||
docker build -t my-code-server-image .
|
||
```
|
||
|
||
## Step 3: Run the Docker Container
|
||
|
||
Run the container, ensuring that the `.bash_history` file's location is mounted outside the container to keep the history persistent.
|
||
|
||
```bash
|
||
docker run -d \
|
||
--name my-code-server-container \
|
||
-p 8443:8443 \
|
||
-v ~/.config:/config # Mount a volume for persistent storage
|
||
my-code-server-image
|
||
```
|
||
|
||
*Note:* Replace `/config` with the actual path where you want to store shell history on your host machine for persistence between container restarts.
|
||
|
||
## Step 4: Verify Shell History Working
|
||
|
||
1. Attach to the running container shell:
|
||
|
||
```bash
|
||
docker exec -it my-code-server-container /bin/bash
|
||
```
|
||
|
||
2. Run a few commands inside the container:
|
||
|
||
```bash
|
||
ls
|
||
echo "Hello, World!"
|
||
```
|
||
|
||
3. Exit and re-enter the container to see if the history persists.
|
||
|
||
```bash
|
||
history
|
||
```
|
||
|
||
If your commands show up when you type `history`, shell history is working correctly.
|
||
|
||
## Troubleshooting
|
||
|
||
- Ensure that the `HISTFILE` path is mounted correctly and writable.
|
||
- Check file permissions to ensure the container user can read and write to the history file.
|
||
- Ensure that the shell being used is `bash`, as this configuration is specific to `bash`.
|
||
|
||
By following this guide, you should be able to have persistent shell history for your Code-Server instance running inside a Docker container.
|
||
```
|
||
This setup ensures that the shell history within the Docker container gets recorded and can be accessed even after the container restarts. Make sure to adjust the paths and configurations as needed for your specific environment. |